Setting Up Top-Level Containers A JFrame Example A JOptionPane Example

J.E.D.I. Swing Component Description JApplet Extends and corresponds to the Applet class in the AWT package. Also slightly incompatible with the Applet class in terms of adding components to this container. JOptionPane Extends JComponent. Provides an easy way of displaying pop-up dialog box. JDialog Extends and corresponds to the Dialog class in the AWT package. Usually used to inform the user of something or prompt the user for an input. JColorChooser Extends JComponent. Allow the user to select a color. Table 22: Some Swing components For the complete list of Swing components, please refer to the API documentation.

7.5.1 Setting Up Top-Level Containers

As mentioned, the top-level containers like JFrame and JApplet in Swing are slightly incompatible with those in AWT. This is in terms of adding components to the container. Instead of directly adding a component to the container as in AWT containers, you have to first get the content pane of the container. To do this, youll have to use the getContentPane method of the container.

7.5.2 A JFrame Example

import javax.swing.; import java.awt.; class SwingDemo { JFrame frame; JPanel panel; JTextField textField; JButton button; Container contentPane; void launchFrame { initialization frame = new JFrameMy First Swing Application; panel = new JPanel; textField = new JTextFieldDefault text; button = new JButtonClick me; contentPane = frame.getContentPane; add components to panel– uses FlowLayout by default panel.addtextField; panel.addbutton; add components to contentPane– uses BorderLayout contentPane.addpanel, BorderLayout.CENTER; frame.pack; causes size of frame to be based on the components frame.setVisibletrue; } public static void mainString args[] { Introduction to Programming II Page 103 J.E.D.I. SwingDemo sd = new SwingDemo; sd.launchFrame; } } Note that the java.awt package is still imported because the layout managers in use are defined in this package. Also, giving a title to the frame and packing the components within the frame is applicable for AWT frames too. Coding Guidelines: Observe the coding style applied in this example as opposed to the examples for AWT. The components are declared as fields, a launchFrame method is defined, and initialization and addition of components are all done in the launchFrame method. We no longer just extend the Frame class. The advantage of using this style would become apparent when we get to event handling. Here is a sample output. Figure 7.8: Running SwingDemo

7.5.3 A JOptionPane Example

import javax.swing.; class JOptionPaneDemo { JOptionPane optionPane; void launchFrame { optionPane = new JOptionPane; String name = optionPane.showInputDialogHi, whats your name?; optionPane.showMessageDialognull, Nice to meet you, + name + ., Greeting..., optionPane.PLAIN_MESSAGE; System.exit0; } public static void mainString args[] { new JOptionPaneDemo.launchFrame; } } See how easy it is ask input from the user. Here is the sample output for the given program. Introduction to Programming II Page 104 J.E.D.I. Figure 7.9: Running JOptionPaneDemo Introduction to Programming II Page 105 J.E.D.I. 8 GUI Event Handling

8.1 Objectives