Simple Java JDialog Example
Java JDialog is a top level window that takes user input.This window has a border and title.The difference between JDialog and JFrame is that JDialog window has no maximize and minimize buttons.
How to declare Java JDialog
This is how to declare class javax.swing.JDialog.
[class JDialog extends Dialog implements WindowConstants, Accessible, RootPaneContainer
]
Java JDialog Constructors
- JDialog()-creates a modeless Dialog with no frame owner or title.
- JDialog(Frame owner)-specifies frame owner but with an empty title.
- JDialog(Frame owner, String s, boolean modal)-creates a Dialog with frame owner,title, and modality.
Java JDialog Example
[
/** * * @author Eric */
import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JDialog; import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class JavaJDialogExample extends JFrame { public JDialog dialog; JavaJDialogExample() { super("Java JDialog Example"); dialog=new JDialog(this,"Java JDialog Example",true); dialog.setLayout(new FlowLayout()); JButton button=new JButton("Close Dialog"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { dialog.hide(); } }); dialog.add(button); dialog.setSize(300,300); dialog.setVisible(true); } public static void main(String args[]) { new JavaJDialogExample(); } }
]