About the javabook2 Package

 

The Swing version of javabook is now available. To distinguish it from the original javabook, the Swing version is named javabook2. If you compare the two versions, you will notice the source code size is much smaller with the javabook2 classes because of the JOptionPane Swing class. The JOptionPane class supports many functionalities for creating different types of common dialogs. Many low-level computations, especially the placement and adjustment of GUI components, I did in the original javabook are now handled by the JOptionPane class and simple layout managers.

What you can do with the original javabook, you can do with the javabook2 package. All you have to do is to replace the import statement from

import javabook.*;

to

import javabook2.*;

In addition to the backward compatibility, the javabook2 package supports new features. One of them is placing a standard icon such as a question mark, a warning mark, and so forth. A default icon is placed in a dialog box if you don't specify anything. You can specify the icon you want by calling the setIcon method (this method is defined in the abstract JavaBookDialog and inherited by MessageBox, InputBox, ListBox, MultiInputBox, and ResponseBox). See the examples below (assume the objects are created properly):

messageBox.show("How are you?");

messageBox.setIcon(MessageBox.WARNING_ICON);
messageBox.show("Watch you step");

/*
Once you set an icon, all subsequent display will use the designated icon.
*/

messageBox.show("Having fun yet",
                MessageBox.QUESTION_ICON);

/*
Passing of icon type is only allowed with MessageBox. The designated icon is used only for this display.
*/

listBox.addItem( "one" );
listBox.addItem( "two" );
listBox.addItem( "three" );
listBox.addItem( "four" );
listBox.addItem( "five" );
int selection = listBox.getSelectedIndex( );

int size = 4;
...
String label[] = new String[size];
for (int i = 0; i < size; i++ ) {
   label[i] = "Input " + i + " " ;
}
multibox.setLabels( label );
String result[] = multibox.getInputs( );

int num =
inBox.getInteger("Enter your age, please:");

rBox.setLabel(ResponseBox.BUTTON1, "One");
rBox.setLabel(ResponseBox.BUTTON2, "Two");
rBox.setLabel(ResponseBox.BUTTON3, "Three");
int result
  = rBox.prompt("How many cups of coffee");

 

The MainWindow class now shows an image file. You can use any image file you want. To place an image at the center of a MainWindow, save your image as javabook2.gif in a GIF format and put this file in a directory that includes the javabook2 subdirectory. For example, if javabook2 is inside the JavaProjects2 directory, then put javabook2.gif in the JavaProjects2 directory. If you don't want an image to show up, simply don't include a file named javabook2.gif in the said directory. For other options, please check the documentation and the source code. You might want to place an image of your school mascot or some relevant information about your course.

The non-dialog classes, such as Format, Clock, SketchPad, and others, in both versions of javabook are essentially the same. I made some cosmetic improvements in javabook2, but there are no major differences.

If you have any suggestions or requests about the javabook2 package, please do not hesitate to send me an email at ctwu@cs.nps.navy.mil

.