Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

MDIDialogBuilder



There are two interfaces which an be used to open a dialog on a GUI application:
  • The MDIDialog interface allows to present a popup dialog. Inn that case you will need to handle the cration of the dialog windown by yourself
  • The MDIDialogBuilder interface allows the framework to handle the creation of the dialog window, delegating all the heavy lifting to the framework

MDIDialog interface

The MDIDialog interface has only one method to open the dialog: Note that the MDIDialog instance must be a subclass of JComponent.

MDIDialogBuilder interface

The MDIDialogBuilder interface simplifies the creation of popup dialogs. The framework will handle the creation of the dialog window, performing all the heavy lifting.

The dialog will have two parts:
  • The content panel of the dialog on the center of the window
  • The apply panel with the buttons to close, appl or cancel the dialog at the bottom of the window
For example:
dialogBuilder

Creation of the MDIDialogBuilder

The API of the MDIDialogBuilder has only one mandatory method to implement:

All the other methods are optional:

Dialog type

The type of the dialog can be specified through the MDIDialogBuilder.getDialogType() method can be:

OK_DIALOG type

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

OK_PRINT_DIALOG type

The MDIDialogBuilder.OK_PRINT_DIALOG is used for a dialog with only an OK button and a Print button:
dialogokprint
If the user clicks on the OK button, the MDIDialogBuilder.apply() method will be called (the default implementation does nothing).

If the user clicks on the Print button, a file chooser will appear allowing the user to select a file where to save the dialog content. The MDIDialogBuilder.getPrintFileExtension() method allows to specify the extension of the file to save (the default is "txt").

After the file selection, the MDIDialogBuilder.print(File) method will be called (the default implementation does nothing).

YES_CANCEL_DIALOG type

The MDIDialogBuilder.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 MDIDialogBuilder.apply() method will be called (the default implementation does nothing).

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

Being notified of user actions

If the user press the "OK" or "Yes" button, the MDIDialogBuilder.apply() method will be called.

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

If the user clicks on the Print button, a file chooser will appear allowing the user to select a file where to save the dialog content. The MDIDialogBuilder.getPrintFileExtension() method wllos to specify the extension of the file to save (the default is "txt"). After the file selection, the MDIDialogBuilder.print(File) method will be called (the default implementation does nothing).

Showing the dialog

The following methods allow to show the dialog:

UNIQUE_INSTANCE dialogs

Using the MDIDialogType.UNIQUE_INSTANCE value for the second argument of the GUIApplication.showDialog(MDIDialogBuilder, MDIDialogType) method will ensure that only one dialog the same class can be opened at the same time:
  • If the MDIDialogBuilder.getID() method returns null (the default), then if will be impossible to open any instance of the particular class implementing the MDIDialogBuilder interface
  • If the MDIDialogBuilder.getID() method does not return null, then if will be impossible to open any instance of the particular class implementing the MDIDialogBuilder interface with the same ID

Example

The following example creates a dialog presenting the width and height of an image.

   public class AnalyseImageMenuBuilder implements MDIDialogBuilder {
      private final BufferedImage image;

      public AnalyseImageMenuBuilder(BufferedImage image) {
        this.image = image;
      }

      @Override
      public short getDialogType() {
        return MDIDialogBuilder.OK_DIALOG;
      }

      @Override
      public String getDialogTitle() {
        return "Analyze Image";
      }

      @Override
      public JComponent createDialogContent() {
        JPanel pane = new JPanel();
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
        pane.add(Box.createVerticalStrut(5));
        pane.add(new JLabel("Width: " + image.getWidth()));
        pane.add(Box.createVerticalStrut(5));
        pane.add(new JLabel("Height: " + image.getHeight()));
      return pane;
      }
   }
To show this dialog, you just need to do, for example in an action:
   public class AnalyzeImageAction extends AbstractMDIAction {
      public AnalyzeImageAction(MDIApplication appli, String name) {
        super(appli, name);
        this.setDescription("Analyze", "Analyze Image");
      }

      @Override
      public void run() throws Exception {
        GUIApplication gui = ((GUIApplication) app);
        BufferedImage image = (BufferedImage) gui.getSelectedProperties().getObject();
        AnalyseImageMenuBuilder builder = new AnalyseImageMenuBuilder(image);
        gui.showDialog(builder);
      }
   }
with the following result:
dialogBuilder

DefaultMDIDialogBuilder class


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

Showing a default message dialog

Main Article: Message dialogs

The GUIApplication.showMessageDialog(String, int, boolean, boolean, String...) method allows to show a simple message dialog.

They use internally the DefaultMDIDialogBuilder class.

Notes

  1. ^ See also opening dialogs for more information

See also


Categories: gui | swing

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