Dialog Boxes In Java

Scotty Moe

You may have seen different applications ( firefox, chrome, windows ) that display messages and warnings in a window or dialog box to user.

Java have class JOptionPane that provide prebuilt dialogs that can be used by programs to show messages.

Example:1

Making program that show message welcome to Trustingeeks.com in a window or dialog.

import javax.swing.JOptionPane;

public class ExampleDialod {
/**

* the first line is used to import class JOprionPane

*  so that we can use in this class

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

JOptionPane.showMessageDialog(null,"Welcome to Trustingeeks.com");

/**

* ShowMessageDialog is a static method

*  and static method can be accessed by

*  class name .method name

*  ShowMessageDialog takes two parameter

*  one determines where to display the window

*  two takes the string to be displayed in the dialog

*/

}

}

Exampl:1 Output

dialobox

Example:2

Making a program that takes Name sd input from user in dialog box and displays the name in another dialog box

import javax.swing.JOptionPane;

public class Example2Dialog {

/**

* the first line is used to import class JOprionPane

*  so that we can use in this class

*/

public static void main(String[] args) {

/**

* asks the user to input

*/

String username =
 JOptionPane.showInputDialog("please enter your name ");

String message =
 String.format("hey how are you %s", username);

/*

* uses the static method in String class to format

*  the message to be displayed

*/

JOptionPane.showMessageDialog(null, message);

/**

* ShowMessageDialog is a static method

*  and static method can be accessed by

*  class name .method name

*  ShowMessageDialog takes two parameter

*  one determines where to display the window

*  two takes the string or variable containing value

*    to be displayed in the dialog

*/

}

}

Example:2 Output

DIALOG BOX

DALIOGBOX

Leave a Comment