Java Swing-II

Home   »    Java   »    Advance Java   »    Java Swing-II

A dialog is a window that is displayed within the context of another window - its parent. Dialog are used to manage input that can't be handled coneniently, selectiing from a range of options for instance, or enabling data to be entered from the keyboard.

Dialogs can also be used for information messages or warnings. Dialogs are defined by the JDialog class in the javax.swing package, and a JDialog object is a specialized sort of a Window. A JDialog object typically contains one or more components for displaying information or allowing data to be entered plus buttons for selection of dialog options, so there's quite a bit of work involved in puttin one together. However, for many of the typical dialogs, the JOptionPane class provides an easy shortcut to creating dialogs.



MODAL AND NON-MODAL DIALOGS

There are two different kinds of dialog boxes i.e. Modal Dialong and Non-Dialog.

Modal Dialog

When a modal dialog is displayed by selecting a menu item or clicking a button, it inhibits the operation of any other window in the application until the dialog is closed Operation of the application cannot until its OK button is clicked.

Modal dialogs that manage input normally have at least two buttons, an OK button that is used to accept whatever input is being entered and then close the dialog and a CANCEL button to just close the dialog and abort. Dialogs that manage input are almost always modal dialogs, simply because no other interactions need be trigged until the user's input is complete.

Non-Modal Dialog

A non-modal dialog can be left on the screen as long as needed, since it doesn't block the interaction with other windows in the application.

Whether a dialog created is modal or non-modal, is determined either by the argument to a dialog class constructor, or by the constructor chosen, since there are three constructors which creates a non-modal by default.

There's a choice of five constructors for a JDialog object:



Constructor
Description
Mode
JDialog()emptyNon-modal
JDialog(Frame parent)emptyNon-modal
JDialog(Frame parent, String title)TitleNon-modal
JDialog(Frame parent, boolean modal)emptyModal(when modal argument is true) Non-modal (when modal argument is false)
JDialog(Frame parent, String title, boolean modal)TitleModal(when modal argument is true) Non-modal (when modal argument is false)


After the JDialog object is created using any of the constructors, the kind of dialog window can be changed by calling the setModal() method for the object. If the argument is specified as true, then the dialog will be modal and false will make it non-modal. The isModal() method can also be used to check whether the JDialog object is true or not.

This is all well, but there is a lot of work involved just to get a dialog with a message displayed. Deriving a class from the JDialog no doubt gives a complete functionality as to how the dialog works, but there is still an easier way to do it. Fortunately, the Swing library contains an intermediate class called JOptionPane, whose static methods enable to create option panes (stand dialog boxes) directly with few lines of code.

Option panes can be used to display a feedback message or confirmation, or to input information to the application. Read More about JOptionPane Class.




Java Swing-II


JOptionPane Class Input Dialog Boxes JSliders
JProgressBars JTables JTabbedPane
JMenu JPopupMenu JToolBar
JFileChooser JScrollBar JScrollPane
JRootPane JTextPane JTree
JSplitPane JDesktopPane JEditorPane






<< Previous Topic
Next Topic >>




1 comment: