What Does Modal and Modeless (Non Modal) Mean in Java

In java, the dialogue boxes in JOptionPane class are very popular. Now when we use dialogue boxes, whether it is message dialogue box, input dialogue or any other,  we may sometimes need to make the parent container (often JFrame or JWindow) not focusable. In another words, we may have to prevent a (parent) component taking input from the user while one of its member component is active. (You may have noticed that when error message boxes are displayed, you cannot click its parent window on many software until you dismiss it by clicking 'OK' or 'close'). Such a property of a member or child component, is called modality. In java, you can use JDialog with or without modality. A JDialog can be either modal or modeless.
A Modal dialog box is a dialog box that blocks input to some other top-level windows in the application. The modal dialog box captures the window focus until it is closed, usually in response to a button press.
A Modeless dialog box is a dialog box that does not block input to any other top level window while it is shown.
See following java code example:



import java.awt.event.*; import javax.swing.*; public class MainWindow extends JFrame { public MainWindow pt; public MainWindow() { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLayout(null); setSize(200,100); JButton clickbutton= new JButton(); clickbutton.setText("Click me"); add(clickbutton); clickbutton.setLocation(10,10); clickbutton.setSize(100,30); pt=this; clickbutton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(pt,"This is a Modal message Dialog box\nMain WIndow is now inaccessible"); JOptionPane.showMessageDialog(null,"This is a Modeless message Dialog box\nMain WIndow is now accessible"); } }); } public static void main(String args[]) { new MainWindow().setVisible(true); } }

If you give a component as first parameter to methods like showMessageDialog, showConfirmDialog, showInputDialog etc, the dialogue box becomes modal, i.e, when the Dialog box gets visible, the provided component become unfocusable since (the dialogue box grabs the focus instead). If you pass null as first parameter, the dialog box becomes modeless (the dialog box does not block input to or prevent focusing any component).

You may also use JOptionPane.showInputDialog(),JOptionPane.showConfirmDialog() etc.Using these static methods in JOptionPane class, you can simply make modal or modeless Dialog boxes. You can also make your own custom Dialogue Boxes by making objects of JDialog class. The JDialog class has many overloaded versions of constructors which will help you to make various dialogue boxes. The following image will give you a glimpse of constructors the class have.

dialog box JDialog dialogue box dialog boxes in java how to show an error message show dialog box display message box in java
Contructors of JDialog

Some people look for java code to make a JFrame modal. The want to display a JFrame modal in another JFrame. In java, it is not possible to make a JFrame modal because JFrame cannot have modal mode. Instead use the JDialog class. You can use it as easy as JFrame and is designed for modality. But you can make a JFrame modal by playing tricks in java. You may call the
setEnabled(false);
method of the parent Frame when you call the
setVisible(true)
method of the supposedly modal JFrame. And also again call
setEnabled(true)
method of parent JFrame while you close the child.

No comments:

Post a Comment