< Prev
Next >



Dialog class




Dialog class is used to create a top-level container Dialog window which contains a set of components.

There are two kinds of Dialog windows -




Constructors of Dialog


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





An example of Modal Dialog window.


import java.awt.*;
import java.awt.event.*;


public class DialogEx2 extends WindowAdapter implements ActionListener
{
Frame frame;
Label label1;
TextField field1;
Button button1, button2, button3;
Dialog d1, d2, d3;

DialogEx2()
{
frame = new Frame("Frame");
button1 = new Button("Open Modal Dialog");


label1 = new Label("Click on the button to open a Modal Dialog");

frame.add(label1);
frame.add(button1);

button1.addActionListener(this);
frame.pack();

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

public void actionPerformed(ActionEvent ae)
{

if(ae.getActionCommand().equals("Open Modal Dialog"))
{
//Creating a non-modeless blocking Dialog
d1= new Dialog(frame,"Modal Dialog",true);
Label label= new Label("You must close this dialog window to use Frame window",Label.CENTER);
d1.add(label);

d1.addWindowListener(this);
d1.pack();
d1.setLocationRelativeTo(frame);
d1.setLocation(new Point(100,100));
d1.setSize(400,200);
d1.setVisible(true);
}
}

public void windowClosing(WindowEvent we)
{
d1.setVisible(false);
}

public static void main(String...ar)
{
new DialogEx2();
}

}

When you run the code, you are presented a window that shows a button asking you to click on it to open a Model Dialog window, shown in Figure 1.

Figure 1


When you click on the this button, you are presented with a Modal Dialog window which should be closed in order to continue with the program and go back to Frame window. Hence, a model Dialog window blocks all the inputs from user to the window in its background, until it is not closed.
Figure 2





An example of Modeless Dialog window.


import java.awt.*;
import java.awt.event.*;


public class DialogEx3 extends WindowAdapter implements ActionListener
{
Frame frame;
Label label1;
TextField field1;
Button button1, button2, button3;
Dialog dg;

DialogEx3()
{
frame = new Frame("Frame");
button1 = new Button("Open Modeless Dialog");


label1 = new Label("Click on the button to open a Modal Dialog");

frame.add(label1);
frame.add(button1);

button1.addActionListener(this);
frame.pack();

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

public void actionPerformed(ActionEvent ae)
{

if(ae.getActionCommand().equals("Open Modeless Dialog"))
{
//Creating a Modeless Dialog which doesn't block the other parts of application
dg= new Dialog(frame,"Modeless Dialog");
Label label= new Label("You need not to close this dialog window to use the Frame Window",Label.CENTER);
dg.add(label);
dg.addWindowListener(this);
dg.pack();
dg.setLocationRelativeTo(frame);
dg.setLocation(new Point(100,100));
dg.setSize(400,200);
dg.setVisible(true);
}
}

public void windowClosing(WindowEvent we)
{
dg.setVisible(false);
}


public static void main(String...ar)
{
new DialogEx3();
}

}

When you run the code, you are presented a window that shows a button asking you to click on it to open a Modeless Dialog window, shown in Figure 1.

Figure 1


When you click on the this button, you are presented with a Modeless Dialog window which need not to be closed in order to continue with the program and go back to Frame window. Hence, unlike Model Dialog window, a Modeless Dialog window doesn't block all the inputs from user until it is not closed.
Figure 2




Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< MenuBar
Label >

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



Please check our latest addition

PYTHON