How to use JDialog class?
Hello friends,
I have completed core java programming and just started advanced java programming. The advanced java programming seems more complicated than core java programming. I am not able to understand the 'JDialog class' of java. I don't have any idea about this java class. Please let me know something about this 'JDialog class' of java. Also provide suitable example for 'JDialog class'. I will appreciate your help.
Re: How to use JDialog class?
The JDialog class is the main class for creating a dialog window. You can use this class to create a custom dialog, or invoke the many class methods in JOptionPane to create a variety of standard dialogs. The JDialog component contains a JRootPane as its only child. The contentPane should be the parent of any children of the JDialog. The same principle holds true for setting layout managers, removing components, listing children, etc. All these methods should normally be sent to the contentPane instead of to the JDialog.
Re: How to use JDialog class?
Hi,
Following example of java will depict you the use of JDialog class
Code:
import javax.swing.JDialog;
import javax.swing.JPanel;
public class AboutDialogDM extends JDialog implements ActionListener
{
public AboutDialog(JFrame parent, String title, String message) {
super(parent, title, true);
if (parent != null) {
Dimension parentSize = parent.getSize();
Point p1 = parent.getLocation();
setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
}
JPanel messagePane01 = new JPanel();
messagePane01.add(new JLabel(message));
getContentPane().add(messagePane01);
JPanel button01Pane01 = new JPanel();
JButton button01 = new JButton("OK");
button01Pane01.add(button01);
button01.addActionListener(this);
getContentPane().add(button01Pane01, BorderLayout.SOUTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
}
public static void main(String[] abn) {
AboutDialog dlg1 = new AboutDialog(new JFrame(), "title", "message");
}
}
Re: How to use JDialog class?
I suggest you to refer following constructors of JDialog class, which are very useful while using it's properties:
- JDialog(Frame ownr, String ttl, boolean mdl, GraphicsConfiguration grcn)
- JDialog(Frame ownr, String ttl, boolean mdl)
- JDialog(Frame ownr, String ttl)
- JDialog(Frame ownr, boolean mdl)
- JDialog(Dialog ownr, String ttl, boolean mdl, GraphicsConfiguration grcn)
Re: How to use JDialog class?
Please review below example of JDialog class:
Code:
class HelpDM extends JDialog
{
Help(Frame frm, String title)
{
super(frm, title);
setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
try {
JEditorPane ep1 = new JEditorPane("file:///" + new File("").getAbsolutePath() + "/uchelp.html");
ep1.setEnabled(false);
getContentPane().add(ep1);
}
catch (IOException ioe) {
JOptionPane.showMessageDialog(frame, "Unable to install editor pane");
return;
}
setSize(300, 200);
setLocationRelativeTo(frm);
setVisible(true);
}
}
Re: How to use JDialog class?
Please study following methods of JDialog class, which will give you something idea about it's use:
- setRootPaneCheckingEnabled(boolean bln)
- setLayout(LayoutManager lytmanager)
- setJMenuBar(JMenuBar jmnu)
- setGlassPane(Component glsPane)
- setDefaultLookAndFeelDecorated(boolean defaultFeelDecorated)
- setContentPane(Container contentPn)