Advertisement



< Prev
Next >



JDialog class




JDialog class is used to create a top-level container Dialog window which contains a set of components like button, text field, label, etc.





There are two kinds of Dialog windows -







Constructors of JDialog


Constructor Description
public JDialog() Creates a modeless JDialog window without a Frame owner or title.
public JDialog(Dialog owner, String title) Creates a modeless JDialog window with a Frame owner and a title.
public JDialog(Dialog owner, String title, boolean modal) Creates a JDialog window with a Frame owner, title and let's you define modality of JDialog window.



Advertisement




An example of JDialog displaying Modeless and Modal JDialog windows.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Dialog2
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A implements ActionListener
{
JFrame jf;
JLabel label1;
JTextField field1;
JButton button1, button2, button3;
JDialog d1, d2, d3;

A()
{
jf = new JFrame("JDialog");
button1 = new JButton("Modal JDialog");
button2 = new JButton("Modeless JDialog");

label1 = new JLabel("Click on any of the buttons for a surprise");

button1.addActionListener(this);
button2.addActionListener(this);

jf.add(label1);
jf.add(button1);
jf.add(button2);


jf.pack();

jf.setLayout(new FlowLayout());
jf.setSize(330,250);
jf.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Modal JDialog"))
{
//Creating a non-modeless blocking JDialog
d1= new JDialog(jf,"Modal Dialog",true);
JLabel label= new JLabel("You first need to close this dialog window to continue");
d1.add(label);
d1.pack();
d1.setLocationRelativeTo(jf);
d1.setLocation(new Point(100,100));
d1.setSize(320,200);
d1.setVisible(true);
}

if(ae.getActionCommand().equals("Modeless JDialog"))
{
//Creating a modeless JDialog which doesn't block the 
d2= new JDialog(jf,"Modeless Dialog");
JLabel label= new JLabel("You don't have to close this dialog window to continue");
d2.add(label);

d2.pack();
d2.setLocationRelativeTo(jf);
d2.setLocation(new Point(100,100));
d2.setSize(320,200);
d2.setVisible(true);
}
}

}

When you run the code, you are presented a window that shows two buttons with two different variants of JDialog windows.

Figure 1


When you click on the first button which says, Modal Dialog, you are presented with a Modal JDialog window which should be closed in order to continue with the program and click on the second button. Hence, a model JDialog window blocks all the inputs from the user to the window in its background, until it is not closed.

Figure 2


When you click on the first button which says, Modeless Dialog, you are presented with a Modeless JDialog window which need not be closed in order to continue with the program and you may click on any button even when Modeless JDialog window on. Hence, a Modeless JDialog window does not block the inputs from the user to the window in its background.

Figure 3




Please share this article -





< Prev
Next >
< Menu Bar and items
JLabel >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement