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 - Combo Boxes (Page 6 of 8 )
A combo box is a GUI element that enables the user of your program to select an item from a list of items contained in a drop-down menu. You create a combo box by declaring an instance of the JComboBox class, as shown here:
A combo box is a GUI element that enables the user of your program to select an item from a list of items contained in a drop-down menu. You create a combo box by declaring an instance of the class, as shown here:
JComboBox combo1 = new JComboBox();
Once the instance is declared, you insert items into the drop-down list by calling the addItem() method defined in JComboBox. The addItem() method requires one argument: the text of the item you want added to the combo box. The following statement inserts the text “One” into the instance of the JComboBox called combo1.
combo1.addItem("One");
Two additional steps are necessary to place the combo box in the container of the window. First, you’ll need to place the combo box in the content pane by calling the add() method of the content pane. Second, you’ll place the content pane in the container by calling the setContentPane() method.
Figure 12-9.A combo box GUI element contains a drop-down list from which the user of your program selects an item.
The following example shows how to create a combo box in a Java program. Figure 12-9 shows the window displayed when you run this 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(); FlowLayout flm = new FlowLayout(); ca.setLayout(flm); ca.setBackground(Color.lightGray); JComboBox combo1 = new JComboBox(); combo1.addItem("One"); combo1.addItem("Two"); combo1.addItem("Three"); ca.add(combo1); setContentPane(ca);
} }
Text Area
The text area GUI element is used to place a block of text in the window. You create a text area by declaring an instance of the JTextArea class, as shown here:
The text area GUI element is used to place a block of text in the window. You create a text area by declaring an instance of the class, as shown here:
JTextArea ta = new JTextArea("Default text",5, 30);
The constructor of the JTextArea requires two arguments: the number of lines and the number of characters that can appear on each line. Programmers refer to the number of lines as the height of the text area and the number of characters as its width. The number of characters you specify is really an approximation made by the Java Virtual Machine because the actual number of characters that fit on a line depends on the font used to display the text.
Another version of the JTextArea() constructor uses three arguments, the first of which is the text that appears in the text area. This is illustrated in the statement at the beginning of this section. The other two arguments are the number of lines and the number of characters, which define the height and width of the text area.
You can place text within the text area by calling the setText() method and passing it the text you want displayed in the text area. This is illustrated in the next statement, where the instance of the JTextArea is called ta:
ta.setText("Default text");
A text area can be used to display text, but you can also use it to have the user of your program enter text or edit text that already appears in the text area. You determine whether the user can edit the text area by calling the setEditable() method and passing it either a Boolean true (to make the text area editable) or a Boolean false (to make the text read-only). This is shown here:
ta.setEditable(true);
Figure 12-10. The text area GUI element is used to display a block of text or to receive a block of text from the user of your program.
The following example shows how to create a text area in a window. This example shows a text area that is five lines high and approximately 30 characters wide. It contains default text. Figure 12-10 shows the window displayed by this 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(); FlowLayout flm = new FlowLayout(); ca.setLayout(flm); ca.setBackground(Color.lightGray); JTextArea ta = new JTextArea("Default text",5, 30); ca.add(ta); setContentPane(ca); } }
Scroll Pane
Sometimes the entire contents of a GUI element won’t fit in the space allocated for the element. This is the case when text exceeds the height of a text area. In order to enable the user to see additional contents, you can use a scroll pane. A scroll pane is a GUI element that enables the user to scroll another GUI component both horizontally and vertically by using a scroll bar.
You create a scroll pane by declaring an instance of the JScrollPane class, as shown here:
JScrollPane sp = new JScrollPane( ta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
The constructor of the JScrollPane class accepts three arguments. The first argument is a reference to the GUI element that will use the scroll bars. In the preceding statement, ta is a reference to a text area GUI element. The second and third arguments are constants of the JScrollPane class that specify the behavior of the vertical and horizontal scroll bars. The preceding statement causes both the vertical and horizontal scroll bars to always appear, even if all the content of the GUI element appears on the screen. Table 12-3 contains the list of constants you can use to set the behavior of the scroll bars.
Constant
Description
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
Displays a vertical scroll bar only
when the contents extend beyond
the area of the GUI element
JScrollPane.VERTICAL_SCROLLBAR_NEVER
Indicates to never use a vertical
scroll bar, even if the contents
extend beyond the area of the GUI element
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
Indicates to always show a vertical scroll bar, even if the
contents do not extend beyond the area of the GUI element
JScrollPane.HORIZONTAL_SCROLLBAR_AS_
Displays a horizontal scroll bar
NEEDED
only when the contents extend
beyond the area of the GUI
element
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
Indicates to never use a horizontal
scroll bar, even if the contents
extend beyond the area of the GUI element
Table 12-3 Constants for Use with the Scroll Pane
The follow example illustrates how to use a scroll pane in your program. This example displays both a vertical and horizontal scroll bar around a text area. Figure 12-11 shows the window displayed by this example.
Figure 12-11.A vertical and a horizontal scroll bar can be added to the text area by using the scroll pane GUI element.
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(); FlowLayout flm = new FlowLayout(); ca.setLayout(flm); ca.setBackground(Color.lightGray); JTextArea ta = new JTextArea("Default text",5, 30); JScrollPane sp = new JScrollPane( ta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); ca.add(sp); setContentPane(ca); } }