Class DialogPanel

  • All Implemented Interfaces:
    ImageObserver, MenuContainer, Serializable, Accessible

    public class DialogPanel
    extends JPanel
    General purpose Dialog with OK and (optionally) Cancel button and/or other buttons, intended to give a unified L&F to all dialogs in the viewer

    Typically you would call addComponent, and if action needs to be done on accept or cancel you would override acceptDialog or cancelDialog. For a no-op on these buttons these should just return, otherwise they should call super.acceptDialog/cancelDialog.

    Here's a fairly typical use case:
     final JTextField field = new JTextField();
     DialogPanel panel = new DialogPanel() {
         public String validateDialog() {
             if (field.getLength() == 0) {
                 return "Field can't be empty";
             }
             return null;
         }
     }
     panel.addComponent(field);
     if (panel.showDialog(pdfviewer)) {
         // Do something with field.getValue();
     }
     
    See Also:
    Serialized Form
    • Constructor Detail

      • DialogPanel

        public DialogPanel()
        Create a new DialogPanel with an OK and Cancel button
      • DialogPanel

        public DialogPanel​(boolean cancancel)
        Create a new DialogPanel
        Parameters:
        cancancel - whether a Cancel button should be added
      • DialogPanel

        public DialogPanel​(boolean canok,
                           boolean cancancel)
        Create a new DialogPanel
        Parameters:
        canok - whether an OK button should be added
        cancancel - whether a Cancel button should be added
    • Method Detail

      • validateDialog

        public String validateDialog()
        Validate the dialog - if the dialog cannot be accepted, this method should return a not-null string. Subclasses should override.
      • addComponent

        public void addComponent​(JComponent component)
        Add a component to the Dialog.
      • addComponent

        public void addComponent​(String label,
                                 JComponent component)
        Add a component to the Dialog with the specified label
      • addButton

        public void addButton​(String name,
                              KeyStroke keystroke,
                              Action action)
        Add a button to the Dialog
        Parameters:
        name - the name of the button (also the button text)
        keystroke - the keystroke to activate the button, or null
        action - the action to perform on the button, or null to remove the existing button
      • addButton

        public void addButton​(String name,
                              String text,
                              KeyStroke keystroke,
                              Action action)
        Add a button to the Dialog
        Parameters:
        name - the name of the button
        text - the text to display on the button
        keystroke - the keystroke to activate the button, or null
        action - the action to perform on the button, or null to remove the existing button
      • setButtonText

        public void setButtonText​(String name,
                                  String text)
        Set the text on the specified button
      • setModal

        public void setModal​(boolean modal)
        Set whether this dialog should be modal (the default) or not
      • cancelDialog

        public void cancelDialog()
        Action to be called when the dialog is cancelled. By default this closes and disposes of the dialog.
      • acceptDialog

        public void acceptDialog()
        Action to be called when the dialog is okayed. By default this calls validateDialog() and if that returns null, close the dialog and set the response to true.
      • showUndecoratedDialog

        public boolean showUndecoratedDialog​(Component root)
        Show the Dialog with no window decorations
        Returns:
        true if the dialog was OK, false otherwise
      • showDialog

        public boolean showDialog​(Component root)
        Show the Dialog with usual window decorations
        Returns:
        true if the dialog was OK, false otherwise
      • showDialog

        public boolean showDialog​(Component root,
                                  String title)
        Show the Dialog with usual window decorations and the specified title
        Returns:
        true if the dialog was OK, false otherwise