Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Façade clears complexity

Develop dialog boxes and Swing apps faster

  • Print
  • Feedback

Page 2 of 5

JOptionPane: Swing's façade for dialogs

Swing provides extensive facilities for windows and dialog boxes, from low-level functionality for creating, populating, and showing windows to high-powered features such as glass panes and choosers for selecting files and colors. Although you can do nearly anything with Swing windows and dialogs, it's not an easy matter to create even the simplest dialogs from scratch; for example, consider Figure 2's application that displays a home-grown message dialog.

Figure 2. A home-grown message dialog

Figure 2's application has a toolbar with two active buttons: if you click the left button, the application creates a message dialog from scratch and displays it; Example 1 lists the event handler for that button.

Example 1. Implement a simple message dialog

   ...
   JDialog dialog = new JDialog(this, "Error!", false);
   ...
      newButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            // Top-left panel:
            JPanel topLeft = new JPanel();
            topLeft.setLayout(new FlowLayout(FlowLayout.LEFT));
            topLeft.add(new JLabel(new ImageIcon("../graphics/stopsign.jpg")));
            // Top-right panel:
            JPanel topRight = new JPanel();
            topRight.setLayout(new BorderLayout());
            topRight.add(new JLabel("You must follow directions!", JLabel.CENTER), BorderLayout.EAST);
            // Top panel:
            JPanel top = new JPanel();
            top.setLayout(new BorderLayout(15,0));
            top.add(topLeft, BorderLayout.WEST);
            top.add(topRight, BorderLayout.EAST);
            // OK button:
            JButton button = new JButton("OK");
            button.setDefaultCapable(true);
            getRootPane().setDefaultButton(button);
            button.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                  dialog.hide();
               }
            });
            // Bottom panel:
            JPanel bottom = new JPanel();
            bottom.setLayout(new BorderLayout());
            bottom.add(button, BorderLayout.EAST);
            // Dialog panel:
            JPanel dialogPanel = new JPanel();
            dialogPanel.setBorder(BorderFactory.createEmptyBorder(15,15,15,10));
            dialogPanel.setLayout(new BorderLayout());
            dialogPanel.add(top, BorderLayout.NORTH);
            dialogPanel.add(bottom, BorderLayout.SOUTH);
            Container cp = dialog.getContentPane();
            
            cp.add(dialogPanel);
            dialog.setResizable(false);
            dialog.pack();
            dialog.setLocationRelativeTo(TLDViewer.this);
            dialog.show();
         }
      });


The preceding code uses five panels (instances of JPanel) to construct the dialog's user interface; Figure 3 shows those panels.

Figure 3. Layout for a dialog

Figure 3's panels are arranged in a hierarchy like this:

dialogPanel
   top
      topLeft
      topRight
   bottom


The dialogPanel contains the top and bottom panels, and the top panel contains the topLeft and topRight panels.

Each panel in Figure 3 uses an appropriate layout manager in Example 1. I wanted the graphic left-justified, and the message and the button right-justified. I picked this configuration because it's the same as message dialogs created by Swing's dialog façade (see "Use JOptionPane" below for more information).

Although it's certainly not rocket science, Example 1 should convince you that creating dialog boxes from scratch is tedious work, which results in frequent trips to the Swing Javadocs for the average Java developer. Of course, wise developers would encapsulate that code in a class of its own for future reuse, instead of reimplementing the same functionality for future dialog boxes. Swing's creators were even wiser: they implemented a class—JOptionPane—that pops up the most common types of dialog boxes. Let's see how we can use JOptionPane to simplify dialog creation and display.

  • Print
  • Feedback

Resources