The Swing-Based javabook Package

 

 

The javabook classes are implemented using the Swing classes. The original version of the javabook package was implemented using the AWT classes, and if you compare the two versions, you will notice the source code size is much smaller with the Swing version because of the JOptionPane 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.

In addition to the backward compatibility, the Swing version 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");