Displaying Text in a Dialog Box

Typically, dialog boxes are windows in which programs display important messages to the
user of the program. Java’s ‘ class JOptionPane ‘ prepackaged dialog boxes that
enable programs to display messages to users.

Java’s numerous predefined classes are grouped into categories of related classes called packages. The packages are referred to collectively as the Java class library, or the
Java applications programming interface (Java API). The packages of the Java API are
split into core packages and extension packages. The names of the packages begin with
either “java” (core packages) or “javax” (extension packages). Many of the core and
extension packages are included as part of the Java 2 Software Development Kit.

Let us write a program first & then understand it step-by-step.
// Welcome.java
// Printing multiple lines in a dialog box

// Java extension packages

import javax.swing.JOptionPane; // import class JOptionPane
public class Welcome {
public static void main( String args[] )
{ JOptionPane.showMessageDialog(null, “Welcome\nto\nJava\nProgramming!” );

System.exit( 0); // terminate application

} // end method main

// end class Welcome ___________________________________________________________________



  • In this example, we use class JOptionPane, which Java defines for us in package javax.swing.
  • In every program that specifies import statements, we separate the import statements into the following groups: Java core packages (for package names starting with java) & Java extension packages (for package names starting with javax)
  • The compiler uses import statements to identify and load classes used in a Java program. When you use classes from the Java API, the compiler attempts
    to ensure that you use them correctly. The import statements help the compiler locate the classes you intend to use. For each new class we use from the Java API, we indicate the
    package in which you can find that class.
  • The rest of the program is similar to New Java Program.


What do you think? Please reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s