Graphical user interfaces allow end users to interact with applications in a more or less intuitive manner. This article explains how to use these components in your own Java programs. It is taken from chapter 12 of the book Java DeMYSTiFieD, written by Jim Keogh (McGraw-Hill/Osborne, 2004; ISBN: 0072254548).
Graphical User Interface - Labels and Text Fields (Page 5 of 8 )
Two of the most commonly used GUI elements are the label and the text field. The label element is used to place text in the container, and the text field element enables the person who uses your program to enter text.
You create a label by declaring an instance of the JLabel class and passing its constructor the text that will appear as the label, as shown here, where “Student Information” is the text of the label:
JLabel lab1 = JLabel("Student Information");
You create a text field by declaring an instance of the JTextField class and passing the constructor of this class two arguments. The first argument is the text that will appear in the text field. The second argument is the number of characters that can be entered into the text field. Some programmers simply leave out the first argument because they’ll use the label GUI element to label the text field instead of placing default text within the text field.
The following example shows how to add a label and text field to a container. Notice that that add() method is called for each GUI element. Once all the elements have been added to the content pane, the setContentPane() method is called to
Figure 12-7.The JLabel class is used create a label, and the JTextField class is used to create a text field.
place the content pane in the container. Figure 12-7 shows the window that is displayed when this example runs.
import java.awt.*; import javax.swing.*; public class Demo { public static void main(String[] args) { Window win = new Window(); } } class Window extends JFrame { public Window () { super ("Window Title"); setSize(400,100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); Container ca = getContentPane(); ca.setBackground(Color.lightGray); FlowLayout flm = new FlowLayout(); ca.setLayout(flm); JLabel lab1 = new JLabel("Student Information"); ca.add(lab1); JTextField text = new JTextField("First Name",25); ca.add(text); setContentPane(ca); } }
Radio Buttons and Check Boxes
Radio buttons and check boxes enable the user of your program to choose a selection rather than having to enter the selection into a text field. Radio buttons are usually displayed in a group. Only one radio button within the group can be selected. All other radio buttons become deselected automatically when the user selects one radio button within the group. Typically, one radio button within the group must be selected. In contrast, check boxes are not grouped together, enabling a user to select none, all, or a combination of check boxes.
You create a radio button by declaring an instance of the JRadioButton class. Each radio button is uniquely identified within the group by a label. You create the label by passing text to the JRadioButton constructor, as shown here:
JRadioButton rb1 = new JRadioButton("Pass");
You must also create a radio button group. You do this by declaring an instance of the ButtonGroup class, as shown here:
ButtonGroup passFail = new ButtonGroup();
You add a radio button to the button group by calling the add() method of the instance of the ButtonGroup class. This is shown in the following statement, where the radio button rb1 is added to the button group passFail:
passFail.add(rb1);
You then add the button group to the content pane by calling the add() method, as shown in previous examples.
You create a check box by declaring an instance of the JCheckBox class and passing its constructor the text that will be used as the label for the check box. This is shown here:
JCheckBox cb1 = new JCheckBox("Completed");
A reference to the check box is passed to the add() method of the content pane in order for the check box to appear in the content pane. This is basically the same step used to pass a reference to the button group to the add() method of the content pane.
Once all the GUI elements are added to the content pane, the setContentPane() method is called and is passed a reference to the content pane.
Figure 12-8.Radio buttons must appear in a button group. Check boxes do not have to appear in a button group.
The following example shows how to display radio buttons and a check box in the content pane of a window. Figure 12-8 shows the window that is displayed when you run the following example:
import java.awt.*; import javax.swing.*; public class Demo { public static void main(String[] args) { Window win = new Window(); } } class Window extends JFrame { public Window () { super ("Window Title"); setSize(400,100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); Container ca = getContentPane(); ca.setBackground(Color.lightGray); FlowLayout flm = new FlowLayout(); ca.setLayout(flm); JCheckBox cb1 = new JCheckBox("Completed"); ButtonGroup passFail = new ButtonGroup(); JRadioButton rb1 = new JRadioButton("Pass"); JRadioButton rb2 = new JRadioButton("Fail"); passFail.add(rb1); passFail.add(rb2); ca.add(cb1); ca.add(rb1); ca.add(rb2); setContentPane(ca); } }