User Interactions using JOptionPanes


Background.  In Visual Basic, there are input boxes and message boxes in order to interact with users in fairly direct and simple ways.  Java has analogous options, but they are based on something called JOptionPanes

It is important to know that Java makes use of many different pre-existing packages/classes to build-in particular functionalities.  For example, the Swing package has been developed to allow developers to write a single set of code that can have a graphical user interface across a variety of platforms.

I mention Swing first, because JOptionPane is a class in the larger Swing package.

The following table lists some of the most frequently used packages and gives a brief description.  They are imported into programs very early in the program.


Package Description
java.applet This package contains the the Applet Package and several interfaces that allow a developer to create applets and other things such as interactions with browsers.
java.awt This is the Java Abstract Windowing Toolkit (unfortunately I tend to call it the Average White Tool which fits too well).  This was developed to enable creation and use of graphical user interfaces in early versions of Java.  It is still in use and we will use it, but we will emphasize Swing.
java.awt.event This package contains classes and interfaces that enable the program to listen for events that occur in a GUI. This is the Java Input/Output Package that enables programs to input and output data
java.lang This is the Java Language Package which is automatically imported in all java programs. This is the Java Networking Package which is used to enable programs to communicate across computer networks.
java.text This is the Java Text Package.  This enables programs to do additional processing and manipulation of numbers, characters, dates and strings.  It also provides many of Java's internationalization capabilities.
java.util This is the Java Utilities Package.  This contains a large variety of utilities for dealing with things like dates and time, random number processing and string manipulation.
javax.swing This is the Java Swing GUI Components Package.  This package contains the classes for implementing Java's Swing GUI components.  We will prefer these to the awt.
javax.swing.JApplet This is the Swing packages classes for Applets.
javax.swing.event These are the classes in Swing for listening and interacting with user's actions on GUIs.


Each package is composed of classes.  Almost all classes will inherit from or extend a parent class or super class.  JOptionPane is no different.  The following diagram shows the hierarchy of classes for JOptionPane in the swing package.



JOptionPane inherits methods and properties from JComponent.  JComponent is a class that has properties and methods applicable to everything that the swing developers considered to be a component for graphical user interfaces.  For example, the following list contains classes of several swing components.
  • JOptionPane
  • JTextField
  • JLabel
  • JTextArea
  • JScrollPane
  • JButton
  • JCheckBox
  • JComboBox

The swing developers didn't not want to repeat the code that was common to all of these.  Much of it, particularly that which is "component-like" should be written once and then grabbed when necessary.  This is one of the biggest advantages of developing classes with methods and properties.

Let's take some time to try and think of properties and methods that will be common to many of these components.

Potential Common Properties for JComponent

  • background colors
  • foreground colors
  • alignment of position
  • height and width

Potential Common Methods for JComponent

  • createToolTip( )
  • foreground colors
  • getFocus( )
  • setFocus( )
  • paintComponent( )
  • getConditionForKeyStroke( )
  • setSize( )

Taking advantage of these kinds of commonalities is one of the goals of object oriented programming.  It takes some time to develop some insight as to how these sorts of hierarchies should be developed.  Plenty of disagreements happen!

Classes, methods, properties and class hierarchies will be something we won't truly dig into until the last several weeks of this semester.  But I want start you thinking and observing in certain ways.

Now we move onto our first two programs, one an application, the other an applet, will give a quick demonstration of two methods within the JOptionPane class in the swing package.  All they do is prompt the user for some input in an showInputDialog and display it in an showMessageDialog.

A Message Box Application.  This java application should be called  Notice how this name and spelling agree with the name of the overall inclusive class.


// An application to get
// inputs from the user
// and display them using
// message boxes

import javax.swing.JOptionPane;

public class MessageBoxApp

public static void main( String args[] )

String userEntry;

// Prompt user for input and read userEntry
userEntry = JOptionPane.showInputDialog("Please enter a word or phrase:");

JOptionPane.showMessageDialog( null, "You entered: " + userEntry, "Your Title", JOptionPane.INFORMATION_MESSAGE);


} // end method main

} // end class MessageBoxApp


Program Discussion.

The lines in green are comments and are created on a line using double slashes.

Notice that every executable  line ends with a semicolon.

Notice the import statement that is the very first line of functioning code.  It brings in the JOptionPane class within the swing package.

The method called main is automatically executed whenever an application is run.

I declare userEntry as a string.  userEntry is obtained from the user based on the showInputDialog statement.  We will say more about this method later in this page.

The showMessageDialog method is used to display whatever the user typed into the input dialog.

The last System.exit(0); statement causes the program to terminate when it is executed after the user enters their input and sees it displayed.

Program Execution.  Now you should go to the directory where your copy of is located.  On my computer this will be

cd c:\java_programs\MessageBoxes

and type


Then, assuming this doesn't give you any errors it creates MessageBoxApp.class in bytecode.  Then you should type

java MessageBoxApp

Which will execute the program and give you the following swing dialog.



I typed in "a word or phrase".

Then when you click on the OK button you should get something like the following.



Click on OK and the message box will disappear and the program will terminate.

The showInputDialog( ) Method.  Now I want to spend a little time describing some of the more general features of the showInputDialog.  The general form for the statement is

showInputDialog(Component parentComponent, Object message, String title, int messageType)

  • You can create a component, such as a combo box, elsewhere in your program and use it to limit user inputs. 
  • You can have a message displayed just like we did. 
  • You can give it a title that will appear in the title bar.
  • You can also give it a message type using Java constants to alter the type of message box and buttons that appear.

We will not be very sophisticated in our use of these options for some time.

The showMessageDialog( ) Method.  The showMessageDialog has similar options, which we will use  somewhat more often.

showMessageDialog(Component parentComponent, Object message, String title, int buttonTypes, int messageType)

Notice how commas are used to delimit each option.

  • In our program the null implies the dialog is positioned in the center of the screen. 
  • The next section causes the phrase "You entered: " to appear just before what you typed is written into the message string by adding on the variable.
    • notice the space after the : in "You entered: "
      • spacing can be very important
    • notice the concatenation of literal strings and variables
      • these sorts of concatenations are used all the time
  • The next section gives the title.
  • You can select which buttons will appear for the user's selection.
  • The last section makes use of a Java constant to determine the type of message box.

A Message Box Applet.  Now we will work essentially the same program only use an applet.  You should notice there are very few differences between the two. 

  • Notice we have an init( ) method rather than a main( ) method. 
  • Notice we also import javax.swing.JApplet
  • Notice we extend JApplet when we declare our inclusive class.
  • There is no System.exit(0) command is required to terminate the program.  The program will terminate when the user closes the applet window.
  • You need to also create a file called MessageBoxApplet.html which is displayed just below.

You should call the following program


// An applet to get
// inputs from the user
// and display them using
// message boxes

import javax.swing.JOptionPane;
import javax.swing.JApplet;

public class MessageBoxApplet extends JApplet

public void init( )

String userEntry;

// Prompt user for input and read userEntry
userEntry = JOptionPane.showInputDialog("Please enter a word or phrase:");

JOptionPane.showMessageDialog( null, "You entered: " + userEntry, "Your Title", JOptionPane.INFORMATION_MESSAGE);


} // end method init

} // end class MessageBoxApplet


Now you want to create another file called MessageBoxApplet.html with the following contents.


<APPLET CODE="MessageBoxApplet.class" WIDTH=400 HEIGHT=200 NAME="Message Boxes">


This is really just HTML using the <Applet> </Applet> tag pair to cause an applet class to be invoked in the browser interpreting the HTML.  We will make use of the Java minimal browser called appletviewer.

Program Execution.  Now you want to do the following to get the program to execute.  It will be barely different other than you will also get an applet window that needs to be closed when you are done.

First, type


to compile the  code and create the  MessageBoxApplet.class in bytecode.

Now you need to type

appletviewer MessageBoxApplet.html

in order to get the dialogs to appear.

When you are done make sure to close the applet window.

Now we will make use of this same sort of approach to work with a variety of data types.