User Defined Dialog Box in Java

User Defined Dialog Box in Java

explore java logo

Table of Contents

a) Introduction to User Defined Dialog Box
b) Steps for creating User Defined Dialog Box
c) Example with Source Code
d) Output

***********************

Introduction

In the previous chapter we discussed about the user defined dialog box theoretically. And in this chapter we are going to create the user defined dialog box.

The steps for creating a user defined dialog box are;

a) JDialog class is used.

b) Two constructor are there, they are: JDialog(JFrame parent, Boolean isModal) and JDialog(JFrame parent, String title, Boolean isModal). You can use any one of them to create user defined dialog box.

Note: First constructor has two parameters and the value should be passed in the constructor. The first parameter should be passed with parent frame and second parameter should be passed with Boolean value that is either true or false.

As we know that Dialog box is like a frame but it cannot execute independently. So we take help from the parent frame. Creating a user defined dialog box is same like creating the frame. Example;

                JDialog d=new JDialog(f,true);
        // size of dialog box
        d.setSize(300,200);
        // default close operation for dialog box
        d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        // set layout to dialog box
        d.setLayout(new FlowLayout());
       
        l=new JLabel("This is a user defined dialog box");
        // add label to the dialog
        d.add(l);
        d.setVisible(true);

If you compare the source code above with the frame its same. Let us see the example of user defined dialog box with source code and output;
I have used the first constructor i.e JDialog(JFrame parent, Boolean isModal) and I have set the value isModal to “true”.

Source Code:

/*
A SWING GUI TO DEMONSTRATE THE USER DEFINED DIALOG BOX.
 */
/**
 *
 * @author AnkitPC
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UserDefinedDialogBoxDemo implements ActionListener{
    JFrame f;
    JButton b;
    JDialog d;
    // constructor
    UserDefinedDialogBoxDemo(){
        f=new JFrame("User Defined Dialog Box");
        f.setSize(400,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        // set layout
        f.setLayout(new FlowLayout());
       
        b=new JButton("Dailog Box");

        // register the event
        b.addActionListener(this);

        // add  component to the frame
        f.add(b);
       
        f.setVisible(true);
    }

    //main method
    public static void main(String[] args) {

        // call constructor
        new UserDefinedDialogBoxDemo();
    }

    // overriding the method of Action Listener interface
    public void actionPerformed(ActionEvent e){
        JLabel l;
        // first constructor with isModal true
        d=new JDialog(f,true);
        // size of dialog box
        d.setSize(300,200);
        // default close operation for dialog box
        d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        // set layout to dialog box
        d.setLayout(new FlowLayout());
       
        l=new JLabel("This is a user defined dialog box");
        // add label to the dialog
        d.add(l);
        d.setVisible(true);
    }
}

Note: Green Color words in the above program is comments. 

Output:
The image below is the initial output of the program.
user defined dialog box
As you know when the isModal parameter has passed with value “true” then the application will not allow accessing other parts of the application.
user defined dialog box
Just watch the video.


Now I have used the second constructor i.e JDialog(JFrame parent,String title, Boolean isModal) and I have set the value isModal to “false”.

Source Code:

/*
A SWING TO DEMONSTRATE THE USER DEFINED DIALOG BOX.
 */

/**
 *
 * @author AnkitPC
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UserDefinedDialogBoxDemo implements ActionListener{
    JFrame f;
    JButton b;
    JDialog d;
    // constructor
    UserDefinedDialogBoxDemo(){
        f=new JFrame("User Defined Dialog Box");
        f.setSize(400,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        // set layout
        f.setLayout(new FlowLayout());
       
        b=new JButton("Dailog Box");
        // register the event
        b.addActionListener(this);
        // add  component to the frame
        f.add(b);
       
        f.setVisible(true);
    }
    //main method
    public static void main(String[] args) {
        // call constructor
        new UserDefinedDialogBoxDemo();
    }

    // overriding the method of Action Listener interface
    public void actionPerformed(ActionEvent e){
        JLabel l;
        // second constructor with isModal true
        d=new JDialog(f,"Demo",false);
        // size of dialog box
        d.setSize(300,200);
        // default close operation for dialog box
        d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        // set layout to dialog box
        d.setLayout(new FlowLayout());
       
        l=new JLabel("This is a user defined dialog box");
        // add label to the dialog
        d.add(l);
        d.setVisible(true);
    }
}

Note: Green Color words in the above program is comments. 

Output:
user defined dialog box

The output you see I same because both program source code is same only I have changed the constructor of the dialog box and value of isModal, this is why I have used the video to show the difference.







Post a Comment

Previous Post Next Post