1. Technology

How to Make a Modeless Dialog Box


A dialog box on a computer screen
SpiffyJ/E+/Getty Images

By default any dialog box made using the JOptionPane class is modal. It makes sense for the majority of dialog boxes that you might want to create. After all, generally you want to grab the user's attention and get a response, or at least an acknowledgement that they've seen your message. But, what if you are happy for the dialog box to stick around on the screen and complement the interface? To do this you need to use the JDialog class.

The JDialog Class

The JDialog class is used to make custom dialog boxes. If you find the options provided in the JOptionPane class too limiting then you can design your own dialog box from scratch. All you need to do is add the components you want to your JDialog object:

   //Creates a frame to be a parent for the JDialog
   JFrame tokenFrame = new JFrame("An example parent frame.");

   //Create some component for the JDialog
   JButton okButton = new JButton("OK");
   JButton cancelButton = new JButton("Cancel");
   JLabel dialogLabel = new JLabel("Quick press a button..");

   //Create the modeless dialog box
   //Using the JFrame tokenFrame as the parent component 
   //allows the dialog to be centered in front of the frame
   JDialog dialog = new JDialog(tokenFrame, "Click a button");
   dialog.setLayout(new FlowLayout());
   //code still needs to be written to determine
   //which button has been pressed.
   //see full code example (link at bottom of article)

Unlike JOptionPane, the JDialog is modeless by default. It gives you the window to build the kind of dialog box you want. This means you need to add the components (e.g., two buttons and a label), decide on the layout manager,  set the size of the dialog and write code to determine what button has been pressed.

Using a Combination of JOptionPane and JDialog

There is a slightly easier way. If you look at the example code, it's clear that apart from its modality the dialog box could be built using the JOptionPane class. Therefore instead of creating my own components I could just combine the JOptionPane and JDialog classes:

   optionPaneDialog = new JDialog(this,"Click a button");
   //Note we are creating an instance of a JOptionPane
   //Normally it's just a call to a static method.
   JOptionPane optPane = new JOptionPane("Let JOptionPane do the work", 
   //Listen for the JOptionPane button click. It comes through as property change 
   //event with the property called "value". 
   optPane.addPropertyChangeListener(new PropertyChangeListener()
      public void propertyChange(PropertyChangeEvent e)
         if (e.getPropertyName().equals("value"))
            switch ((Integer)e.getNewValue())
               case JOptionPane.OK_OPTION:
                  //user clicks OK
               case JOptionPane.CANCEL_OPTION:
                  //user clicks CANCEL
   //Let the JDialog figure out how big it needs to be
   //based on the size of JOptionPane by calling the pack() method

There's two key differences to how you would normally use a JOptionPane. Firstly, you create an instance of the JOptionPane class and add it to the JDialog's content pane. Secondly, you have to write a PropertyChangeListener to find out which button the user clicked. In the above example, I used an inner class to capture the event.

By using the modeless JDialog class we have modified the usual modal behavior of the JOptionPane.

Unless you really need to make a really different kind of dialog box I'd recommend using a combination of the JOptionPane and JDialog class.

A full Java code example can be see in Modeless Dialog Program.

  1. About.com
  2. Technology
  3. Java
  4. Create User Interfaces
  5. Using Dialog Boxes
  6. Java Syntax: How to Make a Modeless Dialog Box

©2014 About.com. All rights reserved.