Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Swing GenericDialog



The GenericDialog class is a generic dialog component, with Yes / No buttons. The minimal method to implement is the GenericDialog.createPanel() method, which allows to create the main panel of the dialog.

The DialogBuilder class class simplifies the creation of popup dialogs, allowing to create several rows of content, optionally enclosed in their titled borders.

Using the Dialog

The basic way to use the dialog involve:

Alternatively, it is possible to create and use the dialog at the same time by:

Note that the default result of clicking the "Yes" or "No" buttons is disposing of the dialog.

Example

Example of a GenericDialogExample() class usage, extending the GenericDialog class:
   public class GenericDialogExample extends GenericDialog {
      private JCheckBox cb = null;

     public GenericDialogExample() {
        this.setDialogTitle("Test");
     }

     @Override
     protected void createPanel() {
        Container pane = dialog.getContentPane();

        pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
        cb = new JCheckBox("My CheckBox");
        pane.add(cb);
     }

     public boolean isSelected() {
        return cb.isSelected();
     }

     public static void main(String[] args) {
        GenericDialogExample dialog = new GenericDialogExample();
        dialog.createDialog(null);
        int ret = dialog.showDialog();
        if (ret == JFileChooser.APPROVE_OPTION) {
           System.out.println("Selected: " + dialog.isSelected());
        }
     }
   }
Result:
genericdialog

Non modal dialog

By default the dialog is modal, but it is possible to use it without being modal. Several constructors of the class have a isModal argument allowing to open a non-modal dialog, for example: GenericDialog(boolean).

When the dialog is opened but is not modal, the listeners registered with the dialog by the GenericDialog.addDialogListener(DialogListener) dialog will be fired with the apply or cancel method depending on if the dialog was applied or cancelled.

Deriving the GenericDialog class

Basic dialog

The minimal method to implement is the GenericDialog.createPanel() method, which allows to create the main panel of the dialog. In this case, the dialog will present:
  • The main panel
  • A bottom bar, with a "Yes", and "Cancel" buttons

It is also advisable to set the title of the Dialog.

Advanced derivation

The simplest way to derive the dialog is to change the Labels for the default "Yes" and "No" buttons (see GenericDialog.setYesNoLabels(String, String). It is possible to change the behavior when clicking these buttons by overriding the GenericDialog.doYes() or GenericDialog.doCancel() methods.

A more advanced derivation is by creating a different bottom panel (see GenericDialog.createYesNoPanel()).

It is also possible to add a menuBar to the dialog.

Deriving the dialog before setting it as visible

You can also override the GenericDialog.applyBeforeShowingDialog(Component) method[1]
The default implementation does nothing
to be able to customize the dialog before setting it as visible. It can for example allow to request the focus on a particular component in the dialog when you show the dialog.

For example:
  public class GenericDialogExample extends GenericDialog {
    private JTextField tf = null;

    public GenericDialogExample() {
       this.setDialogTitle("Test");
    }

    @Override
    protected void createPanel() {
       Container pane = dialog.getContentPane();

       pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
       tf = new JTextField(15);
       pane.add(tf);
    }

     public String getText() {
       return tf.getText();
    }
      
      
       @Override      
        public void applyBeforeShowingDialog() {
          tf.requestFocus();
       }      
      

    public static void main(String[] args) {
       GenericDialogExample dialog = new GenericDialogExample();
       dialog.createDialog(null);
       int ret = dialog.showDialog();
       if (ret == JFileChooser.APPROVE_OPTION) {
          System.out.println("Selected: " + dialog.isSelected());
       }
    }
  }

Listening to the user actions

The GenericDialog.DialogListener interface allows to listen to user actions on the bottom dialog buttons. This listener can be set through the GenericDialog.addDialogListener(DialogListener) method. This interface has several methods, all with a default empty implementation:

Notes

  1. ^ The default implementation does nothing

See also


Categories: packages | swing

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences