Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Dialog builder



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

The dialogs created by this class are instances of the GenericDialog dialog class.

Overview

dialogBuilderOverview
The DialogBuilder is a non-graphical class which allows to configure a dialog.

After the configuration, the builder class allows to create a JDialogBuilderDialog which is a subclass of the GenericDialog class[1]
See GenericDialog for more information about the generic dialog

Configuration of the DialogBuilder

The DialogBuilder class implements the IDialogBuilder interface, so all there is a corresponding setter for all the methods allowing to configure the DialogBuilder:

Dialog type

The type of the dialog can be set through the DialogBuilder.setDialogType(short) method. It can be:

Listening to the user actions

Main Article: GenericDialog

The GenericDialog.DialogListener interface allows to listen to user actions on the bottom dialog buttons.

OK_DIALOG type

The IDialogBuilder.OK_DIALOG is used for a dialog with only an OK button (this is the default option):
dialogBuilder
If the user click on the OK button, the GenericDialog.DialogListener.apply(GenericDialog) method will be called (the default implementation does nothing).

YES_CANCEL_DIALOG type

The IDialogBuilder.YES_CANCEL_DIALOG is used for a dialog with only a Yes button and a Cancel button:
dialogyescancel
If the user clicks on the Yes button, the GenericDialog.DialogListener.apply(GenericDialog) method will be called (the default implementation does nothing).

If the user press the "Cancel" button, the GenericDialog.DialogListener.cancel(GenericDialog)method will be called.

Message type

The DialogBuilder.setMessageType(int) method allows to specify the message type of the dialog, which will specify which icon to present at the left of the dialog. It can be:

Creation of the DialogBuilder content

Adding a part in the DefaultMDIDialogBuilder is done through one of the following methods: The returned DialogBuilder.DialogPart will correspond to one row in the dialog.

Creating the dialog

The following method create the associated JDialogBuilderDialog:

Creating a panel for the dialog content

The following method create a panel containing the dialog content: Using this method allows you to use the dialog content in any dialog window. For example you can use it in the GenericDialog class:
   public class GenericDialogBuilder extends GenericDialog {
     public GenericDialogBuilder() {
        this.setDialogTitle("GenericDialogBuilder");
     }

     @Override
     protected void createPanel() {
       DialogBuilder builder = new DialogBuilder();
       JTextField widthField = new JTextField(10);
       JTextField heightField = new JTextField(10);
       JCheckBox cb = new JCheckBox("Set Size");
       cb.setSelected(true);
       builder.addHorizontalDialogPart("size", "Dimensions", new JLabel("Width"), widthField, new JLabel("Height"), heightField);
       builder.addHorizontalDialogPart(cb);    
       return builder.createDialogContent();
     }
   }

Creating a simple message dialog

The DialogBuilder.createMessageDialog(Component, String, int, boolean, boolean, String...) allow to create a simple message dialog with one or several lines.

For example:
   DialogBuilder builder = new DialogBuilder();
   JDialogBuilderDialog dialog = builder.createMessageDialog(null, "Dialog", IDialogBuilder.INFORMATION_MESSAGE, true, false, "Fist Line", "Second Line");
   dialog.showDialog();      
with the following result:
buildermessageDialog2

Enabling or disabling dialog parts

The two following methods allow to give an id to dialog parts:
  • The first String argument is the id of the corresponding dialog part
  • The second String argument is the border title (can be null)
  • The last argument is the list of componetns in the dialog part
You can enable or disable a dialog part through the JDialogBuilderDialog.enableDialogPart(String, boolean) method.

For example:
   DialogBuilder builder = new DialogBuilder("Set Size");
   JTextField widthField = new JTextField(10);
   JTextField heightField = new JTextField(10);
   JCheckBox cb = new JCheckBox("Set Size");
   cb.setSelected(true);
   builder.addHorizontalDialogPart("size", "Dimensions", new JLabel("Width"), widthField, new JLabel("Height"), heightField);
   builder.addHorizontalDialogPart(cb);      
   JDialogBuilderDialog dialog = builder.createDialog(null);
      
   // enable disable the entire first vertical dialog part
   cb.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        dialog.enableDialogPart("size", cb.isSelected());
      }
   });
   dialog.showDialog();
with the following result:
dialogbuilderparts
This solution uses a component which is outide the dialog part itself. However it is also possible to enable or disable a dialog part with a component inside the dialog part.


After clicking on the checkbox, we have:
dialogbuilderparts2

Using a component in the dialog part itself

The first solution to enable or disable a dialog part uses a component which is outide the dialog part itself. However it is possible to doe the same with a component inside the dialog part, but you need pass this component to the dialog part to avoid to disable it as other components.

The method to do this is the DialogBuilder.DialogPart.setNotDisablingComponent(JComponent). That way the dialog part will not disable or enable the component when calling the JDialogBuilderDialog.enableDialogPart(String, boolean) method.

Note that the component can also be in a dialog part child dialog part.

Customizing dialog parts

It is possible to customize the border of each DialogBuilder.DialogPart.

It is also possible to add dialog parts inside dialog parts. For example:

Example

The following example creates a dialog presenting a width and height of an image:
   BufferedImage image = ...
   DialogBuilder builder = new DialogBuilder("Analyze Image");
   String type = getType(image);
   builder.addVerticalDialogPart("Size", new JLabel("Width: " + image.getWidth()), new JLabel("Height: " + image.getHeight()));
   builder.addVerticalDialogPart("Image Type", new JLabel(type));      
   JDialogBuilderDialog dialog = builder.createDialog(null);
   dialog.showDialog();
with the following result:
dialoginfomessage2

Notes

  1. ^ See GenericDialog for more information about the generic dialog

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