Writing Your Own Dialogs
Home ] Up ] JOptionPane Dialogs ] JFileChooser Dialogs ] JColorChooser Dialogs ] [ Writing Your Own Dialogs ] Modal and Non-Modal Dialogs ] A FontChooser Dialog ]

 

 

The JDialog Class

When you wish to write your own dialog, the first thing you have to do is make your class a subclass of JDialog JDialog provides the basic functionality for dialogs.

Note that the default layout manager for a JDialog's content pane is BorderLayout.

Note also, that a JDialog expects an "owner", or parent, as one of its constructor arguments.  Dialogs are not supposed to be standalone elements (although a null parent effectively makes the dialog's parent be the desktop).

An AboutDialog

Here's a simple AboutDialog class.  It's based on an earlier class we used to show how menu items work, and the AboutDialog is launched when the user clicks on the Help -> About... menu item.

package swingExamples;

import java.awt.Container;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;

class AboutDialog extends JDialog
{
  public AboutDialog(JFrame parent)
  {
    super(parent, "AboutDialog");
    
    // Add components to the dialog's content pane
    Container contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    JLabel label = new JLabel(
              "AboutDialogTest V1", SwingConstants.CENTER);
    contentPane.add(label);
    label = new JLabel(
              "by Bryan J. Higgs", SwingConstants.CENTER);
    contentPane.add(label);
    label = new JLabel(
              "October, 2001", SwingConstants.CENTER);
    contentPane.add(label);
    
    setSize(150, 100);
  }
}

class AboutDialogFrame extends JFrame
{
  public AboutDialogFrame(String title)
  {
    setTitle(title);
    setSize(300, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    // Create menu bar with menu
    JMenu fileMenu = new JMenu("File");
    fileMenu.add("New...");
    fileMenu.add("Open...");
    fileMenu.add("Save");
    fileMenu.add("Save As...");
    fileMenu.addSeparator();
    fileMenu.add("Quit");
    
    JMenu helpMenu = new JMenu("Help");
    JMenuItem aboutItem = new JMenuItem("About...");
    aboutItem.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent ev)
      {
        launchAboutDialog();
      }
    }
    );
    helpMenu.add(aboutItem);
    
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(fileMenu);
    menuBar.add(helpMenu);
    setJMenuBar(menuBar);
  }
  
  private void launchAboutDialog()
  {
    AboutDialog dialog = new AboutDialog(this);
    dialog.setVisible(true);
  }
}

public class AboutDialogExample
{
  public static void main(String[] args)
  {
    AboutDialogFrame frame = 
          new AboutDialogFrame("AboutDialogTest");
    frame.setVisible(true);
  }
}

Here is what results:

 

This page was last modified on 02 October, 2007