11
11
JDialog is a part Java swing package. The main purpose of the dialog is to add components to it.
JDialog can be customized according to user need .
Constructor of the class are:
1. JDialog() : creates an empty dialog without any title or any specified owner
3. JDialog(Frame o, String s) : creates an empty dialog with a specified frame as its owner
and a specified title
5. JDialog(Window o, String t) : creates an empty dialog with a specified window as its owner
and specified title.
7. JDialog(Dialog o, String s) : creates an empty dialog with a specified dialog as its owner and
specified title.
4. isVisible(boolean b): sets the visibility of the dialog, if value of the boolean is true then
visible else invisible
17. repaint(long time, int x, int y, int width, int height): Repaints the specified rectangle of this
component within time milliseconds.
18. remove(Component c): Removes the specified component from the dialog.
19. isRootPaneCheckingEnabled() : Returns whether calls to add and setLayout are forwarded
to the contentPane or not .
20. getTransferHandler() : returns the transferHandler property.
23. createRootPane() : Called by the constructor methods to create the default rootPane.
24. addImpl(Component co, Object c, int i) : Adds the specified child Component to the dialog.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
// frame
static JFrame f;
// main class
f = new JFrame("frame");
// create a object
// create a panel
b.addActionListener(s);
p.add(b);
f.add(p);
f.setSize(400, 400);
f.show();
String s = e.getActionCommand();
if (s.equals("click")) {
// create a label
d.add(l);
// setsize of dialog
d.setSize(100, 100);
d.setVisible(true);
Output:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
// frame
static JFrame f;
// dialog
// main class
f = new JFrame("frame");
// create a object
// create a panel
b.addActionListener(s);
p.add(b);
f.add(p);
f.setSize(400, 400);
f.show();
String s = e.getActionCommand();
if (s.equals("click")) {
// create a label
// create a button
b.addActionListener(this);
// create a panel
JPanel p = new JPanel();
p.add(b);
p.add(l);
d.add(p);
// setsize of dialog
d.setSize(200, 200);
d.setVisible(true);
// create a label
d1.add(l);
// setsize of dialog
d1.setSize(200, 200);
d1.setLocation(200, 200);
d1.setVisible(true);
Output :
Note : The above programs might not run in an online compiler please use an offline IDE