How To Create Dialog Box In Java
Java Swing | JDialog with examples
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:            
- JDialog() : creates an empty dialog without any title or any specified owner
- JDialog(Frame o) :creates an empty dialog with a specified frame as its owner
-               JDialog(Frame o, String s)              : creates an empty dialog with a specified frame as its owner
 and a specified title
- JDialog(Window o) : creates an empty dialog with a specified window as its owner
- JDialog(Window o, String t) : creates an empty dialog with a specified window as its owner and specified title.
- JDialog(Dialog o) :creates an empty dialog with a specified dialog as its owner
- JDialog(Dialog o, String s) : creates an empty dialog with a specified dialog as its owner and specified title.
            Commonly used methods            
Attention reader! Don't stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course .
- setLayout(LayoutManager m) : sets the layout of the dialog to specified layout manager
- setJMenuBar(JMenuBar m) : sets the menubar of the dialog to specified menubar
- add(Component c): adds component to the dialog
- isVisible(boolean b): sets the visibility of the dialog, if value of the boolean is true then visible else invisible
- update(Graphics g) : calls the paint(g) function
- remove(Component c) : removes the component c
- getGraphics() : returns the graphics context of the component.
- getLayeredPane() : returns the layered pane for the dialog
- setContentPane(Container c) :sets the content pane for the dialog
- setLayeredPane(JLayeredPane l) : set the layered pane for the dialog
- setRootPane(JRootPane r) : sets the rootPane for the dialog
- getJMenuBar() : returns the menubar of the component
- setTransferHandler(TransferHandler n) : Sets the transferHandler property, which is a mechanism to support transfer of data into this component.
- setRootPaneCheckingEnabled(boolean enabled) : Sets whether calls to add and setLayout are forwarded to the contentPane.
- setRootPane(JRootPane root) :Sets the rootPane property of the dialog.
- setGlassPane(Component glass) : Sets the glassPane property of the dialog.
- repaint(long time, int x, int y, int width, int height): Repaints the specified rectangle of this component within time milliseconds.
- remove(Component c): Removes the specified component from the dialog.
- isRootPaneCheckingEnabled() : Returns whether calls to add and setLayout are forwarded to the contentPane or not .
- getTransferHandler() : returns the transferHandler property.
- getRootPane() : Returns the rootPane object for this dialog.
- getGlassPane() : Returns the glassPane object for this dialog.
- createRootPane() : Called by the constructor methods to create the default rootPane.
- addImpl(Component co, Object c, int i) : Adds the specified child Component to the dialog.
            The following programs will illustrate the use of JDialog
            1 .Program to create a simple JDialog
Java
                import                java.awt.event.*;              
                import                java.awt.*;              
                import                javax.swing.*;              
                class                solve                                extends                JFrame                                implements                ActionListener {              
                                static                JFrame f;              
                                public                static                void                main(String[] args)              
                                {              
                                f =                                new                JFrame(                "frame"                );              
                                solve s =                                new                solve();              
                                JPanel p =                                new                JPanel();              
                                JButton b =                                new                JButton(                "click"                );              
                                b.addActionListener(s);              
                                p.add(b);              
                                f.add(p);              
                                f.setSize(                400                ,                                400                );              
                                f.show();              
                                }              
                                public                void                actionPerformed(ActionEvent e)              
                                {              
                                String s = e.getActionCommand();              
                                if                (s.equals(                "click"                )) {              
                                JDialog d =                                new                JDialog(f,                                "dialog Box"                );              
                                JLabel l =                                new                JLabel(                "this is a dialog box"                );              
                                d.add(l);              
                                d.setSize(                100                ,                                100                );              
                                d.setVisible(                true                );              
                                }              
                                }              
                }              
            Output:
             
          
            2. Program to create a dialog within a dialog            
Java
                import                java.awt.event.*;              
                import                java.awt.*;              
                import                javax.swing.*;              
                class                solve                                extends                JFrame                                implements                ActionListener {              
                                static                JFrame f;              
                                static                JDialog d, d1;              
                                public                static                void                main(String[] args)              
                                {              
                                f =                                new                JFrame(                "frame"                );              
                                solve s =                                new                solve();              
                                JPanel p =                                new                JPanel();              
                                JButton b =                                new                JButton(                "click"                );              
                                b.addActionListener(s);              
                                p.add(b);              
                                f.add(p);              
                                f.setSize(                400                ,                                400                );              
                                f.show();              
                                }              
                                public                void                actionPerformed(ActionEvent e)              
                                {              
                                String s = e.getActionCommand();              
                                if                (s.equals(                "click"                )) {              
                                d =                                new                JDialog(f,                                "dialog Box"                );              
                                JLabel l =                                new                JLabel(                "this is first dialog box"                );              
                                JButton b =                                new                JButton(                "click me"                );              
                                b.addActionListener(                this                );              
                                JPanel p =                                new                JPanel();              
                                p.add(b);              
                                p.add(l);              
                                d.add(p);              
                                d.setSize(                200                ,                                200                );              
                                d.setVisible(                true                );              
                                }              
                                else                {                              
                                d1 =                                new                JDialog(d,                                "dialog Box"                );              
                                JLabel l =                                new                JLabel(                "this is second dialog box"                );              
                                d1.add(l);              
                                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            
How To Create Dialog Box In Java
Source: https://www.geeksforgeeks.org/java-swing-jdialog-examples/
Posted by: silvawint1952.blogspot.com

0 Response to "How To Create Dialog Box In Java"
Post a Comment