Computer Science 161
Fall 2008
Laboratory exercise #3
Thursday, November 6, 2008
This laboratory exercise will give us a chance to use some of the basic windows operations available in Java.
Using the Scanner class is really neat, but it is not the way that most windows based programs behave. In this second laboratory session, we will look at a simple method to communicate with the user using dialog boxes.
Begin by opening up JOptionPane01. Notice the import statement, with imports from a new package: javax.swing. javax.swing contains a series of (relatively) easy-to-use graphics elements.
Notice that we do not need any class variables, and that the constructor does nothing. Pretty much all we want to do is done in the body of methods.
Important note: When running any examples, please be sure that your desktop is cleared of open windows (minimize them). I think that the dialog boxes should appear in the middle of your screen (you can change that later), but they tend to hide behind other open windows.
Let's begin with a simple example: Write a public void method called simpleNote which displays a very basic information window. The text for doing this is
JOptionPane.showMessageDialog(null, "Here is a message");
This is all we need to do. There are other forms of the showMessageDialog method. The arguments to this one has the following arguments:
Try this out. Please note that in the JOptionPaneExample example I simply created one method called runExamples() into which I put all of these examples. Please feel free to do the same, adding code to this method as you go along.
Next we consider how your program can provide the user with a yes/no/cancel option. Write a second method (you pick the name this time) with the line
int answer = JOptionPane.showConfirmDialog(null,
"Do you really want to delete this?",
"Confirm Delete",
JOptionPane.YES_NO_CANCEL_OPTION);
Note several things:
JOptionPane.showConfirmDialog returns an integer value. These values are given by constants in JOptionPane and the possible results are
These are constants defined in JOptionPane (hence the JOptionPane - dot - option and can be used in your code, for example, by statements such as
if (answer == JOptionPane.YES_OPTION)
{stuff to do if the user has clicked on the "YES" button}
Putting a null as the first parameter is once again a note that JOptionPane should place the dialog box somewhere on the screen. Look at the documentation to see how to control where the pane will appear (we will not need to do this in this class)
The message is usually a String, as before.
The title is a String and will appear at the top of the dialog box. Use a title which says something about the dialog.
optionType is an integer constant defined (as above) in JOptionPane. The possible option types are
There are actually several different forms of the showConfirmDialog. After you get an example running, go look in the documentation for JOptionPane to look for others. The documentation includes a link to a tutorial you might want to look at for advanced options.
When you get this to work, write a JOptionPane.showMessageDialog to display the integer returned. Try using an if-then statement with the options in (1) above to see how to use the provided constants as in (1) above.
Finally (for now at least), either create a new method or add the following line to an existing method:
String anAnswer =
JOptionPane.showInputDialog(null, "Please enter your name");
This should create a dialog box with a field into which you can type a string of characters (you do not need to put quotes around the string you type in. To check that this worked, follow the code above with
JOptionPane.showMessageDialog(null,
"Hello, " + anAnswer +"!",
"WelcomeMessage",
JOptionPane.INFORMATION_MESSAGE);
This is another form of the ShowMessageDialog method. The new field is int messageType, and again we use one of the built-in constants
JOptionPane.ERROR_MESSAGE
JOptionPane.INFORMATION_MESSAGE
JOptionPane.WARNING_MESSAGE
JOptionPane.QUESTION_MESSAGE
JOptionPane.PLAIN_MESSAGE
Experiment with these various options.
One problem with this last example. We can only retrieve Strings from it. What do we do when we want to retrieve a number from it? The solution is to use what is called a "wrapper class" described in our textbook on page 254 and also on pages 436 - 437. The only thing we need from this class is a method: Integer.valueOf(String s). This is a static method, which means that we do not need to have any object associated with it and can simply use it is a function which accepts a string of digits and returns the integer represented by that string. Similar routines exist for Double, etc. More on static at a later time (though do take note of our use of it in declaring a main program). For now, try the following:
anAnswer =
JOptionPane.showInputDialog(null, "Please enter an integer");
int k = Integer.valueOf(anAnswer);
JOptionPane.showMessageDialog(null,
"You entered " + k +"!",
"Trying out String -> integer conversion",
JOptionPane.INFORMATION_MESSAGE);
All of this can be found in JOptionPaneExample in the handouts folder.
In the last lab exercise, you modified your Inventory class to introduce a removePart(int index) method which checked the index, retrieved the part to be removed, retrieved the name of the part to be removed, and then printed a message displaying the part name and asking the user if that was the part to be removed. You then used the Scanner class to accept a yes or no, and then performed the action indicated (no action if the user did not confirm) printing a message either way.
Modify the removePart method of the Inventory class again, but this time