Java 2 Help Essay

Submitted By sassiekassie29
Words: 977
Pages: 4

Gaddis, T. (2011). Starting Out with Java (4th ed.). : Addison-Wesley.

Java Helps

Javax.swing = Swing is the lite version
Java.awt = AWT (Abstract Windowing Toolkit) is the heavyweight version
JOptionPane class allows you to quickly display a dialog box showMessageDialog = display a message dialog
Method
Description void showMessageDialog(Component parent, Object message

This method displays a message dialog. The argument passed into parent is a reference to the graphical component that the dialog box should be displayed within. If you pass null to this parameter, the dialog box appears in the center of the screen. The object passed to the message parameter contains the message that is to be displayed. void showMessageDialog(Component parent, Object message, String title, int messageType)

This method displays a message dialog. The argument passed into parent is a reference to the graphical component that the dialog box should be displayed within. If you pass null to this parameter, the dialog box appears in the center of the screen. The object passed to the message parameter contains the message to be displayed. The string passed to the title parameter is displayed in the dialog box’s title bar. The value passed to messageType indicates the type of icon to display in the message box.

JOptionPane.showMessageDialog(null, "Hello World");
In this statement we pass null as the first argument. This causes the dialog box to be displayed in the center of the screen.

Notice that by default the dialog box in Figure 11-3 has the string “Message” displayed in its title bar, and an information icon (showing the letter “i”) is displayed. You can control the text displayed in the title bar and the type of icon displayed with the second version of the showMessageDialog method. Here is an example:
JOptionPane.showMessageDialog(null, "Invalid Data", "My Message Box", JOptionPane.ERROR_MESSAGE);

In this method call, the third argument is a string displayed in the dialog box’s title bar. The fourth argument is a constant that specifies the type of message being displayed, which determines the type of icon that appears in the dialog box. The constant JOptionPane.ERROR_MESSAGE specifies that an error icon is to be displayed.

The constants that you may use for the message type are JOptionPane.ERROR_MESSAGE, JOptionPane.INFORMATION_MESSAGE, JOptionPane.WARNING_MESSAGE, JOptionPane. QUESTION_MESSAGE, and JOptionPane.PLAIN_MESSAGE. The following statements call the method with each type of message.
// Display an error message.
JOptionPane.showMessageDialog(null, "Error Message", "Error", JOptionPane.ERROR_MESSAGE);
// Display an information message.
JOptionPane.showMessageDialog(null, "Information Message", "Information", JOptionPane.INFORMATION_MESSAGE);
// Display a warning message.
JOptionPane.showMessageDialog(null, "Warning Message", "Warning", JOptionPane.WARNING_MESSAGE);
// Display a question message.
JOptionPane.showMessageDialog(null, "Question Message", "Question", JOptionPane.QUESTION_MESSAGE);
// Display a plain message.
JOptionPane.showMessageDialog(null, "Plain Message", "Message", JOptionPane.PLAIN_MESSAGE);

The JFC, AWT, and Swing
Java programmers use the Java Foundation Classes (JFC) to create GUI applications. The JFC consists of several sets of classes, many of which are beyond the scope of this book. The two sets of JFC classes that we focus on are the AWT and Swing classes. First, we discuss the differences between these two.
Java has been equipped, since its