Wednesday, May 21, 2014

how to use dialog box in java?

A Dialog window is an independent subwindow meant to carry temporary notice apart from the main Swing Application Window. Most Dialogs present an error message or warning to a user, but Dialogs can present images, directory trees, or just about anything compatible with the main Swing Application that manages them.

For convenience, several Swing component classes can directly instantiate and display dialogs. To create simple, standard dialogs, you use the JOptionPane class. The ProgressMonitor class can put up a dialog that shows the progress of an operation. Two other classes, JColorChooser and JFileChooser, also supply standard dialogs. To bring up a print dialog, you can use the Printing API. To create a custom dialog, use the JDialog class directly.

The code for simple dialogs can be minimal. For example, here is an informational dialog:

Here is the code that creates and shows it:

JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");

Below is the code to show dialog box by clicking on button:         

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

//import javax.swing.border.EmptyBorder;

import javax.swing.JLabel;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class Jdialog extends JFrame {

            private JPanel contentPane;

            private javax.swing.JTextField textField;

            private javax.swing.JTextField textField_1;


             * Launch the application.


            public static void main(String[] args) {

                                                     Jdialog frame = new Jdialog();




             * Create the frame.


            public Jdialog() {

                        setTitle("JTextField example");


                        setBounds(100, 100, 450, 300);

                        contentPane = new JPanel();

                        //contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));




                        textField = new javax.swing.JTextField();

                        textField.setBounds(228, 43, 121, 20);




                        JLabel lblEnterYourName = new JLabel("Enter your Name");

                        lblEnterYourName.setBounds(40, 40, 102, 27);



                        JLabel lblEnterAddress = new JLabel("Enter Address");

                        lblEnterAddress.setBounds(40, 102, 86, 27);



                        textField_1 = new javax.swing.JTextField();

                        textField_1.setBounds(228, 105, 121, 20);




                        final JButton btnSubmit = new JButton("Submit");

                        btnSubmit.addActionListener(new ActionListener() {

                                    public void actionPerformed(ActionEvent arg0) {

                                                String txtfld1=textField.getText();

                                                String txtfld2=textField_1.getText();



                                                //String arg = arg0.getActionCommand(); //Return clicked button name

                                                //if (arg.equals("btnSubmit"))

                                                JOptionPane.showMessageDialog(Jdialog.this,"Your name is "+txtfld1+" and address is "+txtfld2,"MessageBox",1);




                        btnSubmit.setBounds(179, 198, 75, 23);





No comments:

Post a Comment