|
|
|
|
Modal DialogsYou've probably experienced dialog boxes in Microsoft Windows, or some other windowing system. Typically, you click on a menu item in the menu bar, or perhaps a button somewhere in the GUI of an application, and up comes a dialog box for you to interact with. Most often, when one of these dialog boxes comes up, you find that you can only interact with it, and cannot interact with the rest of the application. If you try to, say, click on the main window of your application while the dialog box is up, you'll find that it doesn't respond (except that the system may beep at you, to indicate that you're trying to do something that isn't allowed. This kind of dialog box is called a modal dialog. The idea is that the application wants you to supply the information requested in the dialog before you are allowed to do anything else in the program. So it prevents you from interacting with the rest of the application, for that reason. This is implemented by the call to Non-Modal (or Modeless) DialogsSometimes, however, applications need to be able to bring up a dialog box
and have it stay visible, while at the same time interacting with the rest of
the application. Some applications, such as sophisticated
graphical editors like Adobe PhotoShop, operate with several dialogs visible
at any given time. In these applications, the user is expected to
enter information into these dialogs to control what is happening during the
design process. To be useful, such dialogs need to be non-modal
(sometimes known as modeless). That is, they do not block on their
|
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", false); // Non-modal
// 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 AboutDialogNonModalExample
{
public static void main(String[] args)
{
AboutDialogFrame frame =
new AboutDialogFrame("AboutDialogTest");
frame.setVisible(true);
}
}
|
It produces the following results:

As you can see, because the AboutDialog is now non-modal, you can create as
many instances of it as you would like, because you can still interact with the application
frame's menu.
|
This page was last modified on 02 October, 2007 |