DialogBox in java Swing





Dialog Box Tutorial



Introduction:-
A Dialog window is an independent subwindow meant to carry temporary notice apart from the main Swing Application Window. Message dialog box is used to display informative messages to the user. To create simple, standard dialogs, you have to use the JOptionPane class.


Here is the code that creates and shows it:


JOptionPane.showMessageDialog(frame, "Example of Dialog Window.");

Some Methods of JOptionPane class to create Dialog box:-


JOptionPane class is available in the javax.swing.*; package. This class provide various types of message dialog box as follows:


1. showMessageDialog()


Displays a modal dialog with one button, which is labeled "OK" (or the localized equivalent). You can easily specify the message, icon, and title that the dialog displays. Here are some examples of using showMessageDialog: 


1. Default title and icon:-It is used to create an information-message dialog titled "Message”.

JOptionPane.showMessageDialog(frame,"method 1.");


2.Following method is used to create a message dialog with given title and messageType.:-

static void showMessageDialog(Component parentComponent, Object message, String title, int messageType);


2.ShowConfirmDialog(): 


And the last or third method is the showConfirmDialog() which asks the user for confirmation (Yes/No) by displaying message. This method return a numeric value either 0 or 1. If you click on the "Yes" button then the method returns 1 otherwise 0.
Syntax:-

static int showConfirmDialog(Component parentComponent, Object message)


3.ShowInputDialog():


It is used to show a question-message dialog requesting input from the user parented to parentComponent.
Syntax:-

static String showInputDialog(Component parentComponent, Object message)



//program to illustrate the use of Showmessage Dialog method

import javax.swing.*;

public class Message {

    JFrame f;

    Message(){

    f=new JFrame(); 

    JOptionPane.showMessageDialog(f,"This is the example of  showMessageDialog.");

    }

    public static void main(String args[])

    {

     new Message();  

    }

   }

Output:-


//program to illustrate the use of ShowConfirm Dialog method

import javax.swing.*;

public class Message {

    JFrame f;

    Message(){

    f=new JFrame(); 

    JOptionPane.showConfirmDialog(f,"This is the example of showConfirmDialog.");

    }

    public static void main(String args[])

    {

     new Message();  

    }

    }

Output:-


//program to illustrate the use of showInputDialog method

import javax.swing.*; 

public class inputdialog {

    JFrame f;

    inputdialog()

    {

    f=new JFrame();  

    String name=JOptionPane.showInputDialog(f,"Give any suggestion!");     

    }

    public static void main(String[] args) { 

    new inputdialog(); 

} 

}

Output :-




All these programs are for creating a dialog box ,they will not  perform any task.




for any query feel free to comment😊.

Comments

Post a Comment