Using Yes/No Option Dialogues in Applets

The fairly straightforward code of the Yes/No option applet follows a pair of screenshots for the running applet.

Yes/No option dialogue applet (280 x 180) in action

Yes/No option dialogue applet (280 x 180) in action

Second dialogue

Second dialogue

namespace yn_option_dialog_demo;

interface

uses
  java.util, java.applet.*, java.awt.*, javax.swing;
type
  YNOptionDialogDemo = public class(Applet, ActionListener)
  private
    btnOption : Button;
  public
    method init; override;
    method actionPerformed(e : ActionEvent);   
  end;

implementation

method YNOptionDialogDemo.init;
begin
  Background := Color.green.darker;
  btnOption := new Button('Option Dialog');
  add(btnOption);
  btnOption.addActionListener(self);
end;

method YNOptionDialogDemo.actionPerformed(e : ActionEvent);
var
  button_code : Integer;
begin
  if e.getSource = btnOption then
    begin 
      button_code := JOptionPane.showConfirmDialog(self, 'Choose Yes or No', 'Choose Yes or No',
                                                   JOptionPane.YES_NO_OPTION);
      if button_code = 0 then 
        JOptionPane.showMessageDialog(self, '"Yes" button pressed');
      if button_code = 1 then 
        JOptionPane.showMessageDialog(self, '"No" button pressed');
    end;
end;

end.


Other useful built-in button options are JOptionPane.YES_NO_CANCEL_OPTION and JOptionPane.OK_CANCEL_OPTION. See the next page for customised button names.
Programming - a skill for life!

How to use Dialogues in applets in Oxygene for Java