User Interactions using JOptionPanes
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
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.
|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.|
|java.io||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.|
|java.net||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.
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.
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 MessageBoxApp.java. 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
public class MessageBoxApp
} // end class MessageBoxApp
Program Execution. Now you should go to the directory where your copy of MessageBoxApp.java is located. On my computer this will be
Then, assuming this doesn't give you any errors it creates MessageBoxApp.class in bytecode. Then you should type
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)
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.
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.
You should call the following program MessageBoxApplet.java.
|// An applet to get
// inputs from the user
// and display them using
// message boxes
public class MessageBoxApplet extends JApplet
} // 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
|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
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.
to compile the code and create the MessageBoxApplet.class in bytecode.
Now you need to type
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.