We are finally ready to start introducing the Swing user interface components. We begin with the components that let a user input and edit text. You can use the JTextFietd and JTextArea components for text input. A text field can accept only one line of text; a text area can accept multiple lines of text. A JPasswordFietd accepts one line of text without showing the contents.
All three of these classes inherit from a class called JTextComponent. You will not be able to construct a JTextComponent yourself because it is an abstract class. On the other hand, as is so often the case in Java, when you go searching through the API documentation, you may find that the methods you are looking for are actually in the parent class JTextComponent rather than the derived class. For example, the methods that get or set the text in a text field or text area are actually in JTextComponent.
1. Text Fields
The usual way to add a text field to a window is to add it to a panel or other container—just as you would add a button:
var panel = new JPanel();
var textField = new JTextField(“Default input”, 20);
panel.add(textField);
This code adds a text field and initializes it by placing the string “Default input” inside it. The second parameter of this constructor sets the width. In this case, the width is 20 “columns.” Unfortunately, a column is a rather imprecise measurement. One column is the expected width of one character in the font you are using for the text. The idea is that if you expect the inputs to be n characters or less, you are supposed to specify n as the column width. In practice, this measurement doesn’t work out too well, and you should add 1 or 2 to the maximum input length to be on the safe side. Also, keep in mind that the number of columns is only a hint to the AWT that gives the preferred size. If the layout manager needs to grow or shrink the text field, it can adjust its size. The column width that you set in the JTextFietd constructor is not an upper limit on the number of characters the user can enter. The user can still type in longer strings, but the input scrolls when the text exceeds the length of the field. Users tend to find scrolling text fields irritating, so you should size the fields generously. If you need to reset the number of columns at runtime, you can do that with the setCotumns method.
In general, users add text (or edit an existing text) in a text field. Quite often these text fields start out blank. To make a blank text field, just leave out the string as a parameter for the JTextFietd constructor:
var textField = new JTextField(20);
You can change the content of the text field at any time by using the setText method from the JTextComponent parent class mentioned in the previous section. For example:
textFietd.setText(“HeUo!”);
And, as was mentioned in the previous section, you can find out what the user typed by calling the getText method. This method returns the exact text that the user has typed. To trim any extraneous leading and trailing spaces from the data in a text field, apply the trim method to the return value of getText:
String text = textField.getTextO.trimO;
To change the font in which the user text appears, use the setFont method.
2. Labels and Labeling Components
Labels are components that hold text. They have no decorations (for example, no boundaries). They also do not react to user input. You can use a label to identify components. For example, unlike buttons, text fields have no label to identify them. To label a component that does not itself come with an identifier:
- Construct a JLabel component with the correct text.
- Place it close enough to the component you want to identify so that the user can see that the label identifies the correct component.
The constructor for a JLabel lets you specify the initial text or icon and, optionally, the alignment of the content. Use constants from the SwingConstants interface to specify alignment. That interface defines a number of useful constants such as LEFT, RIGHT, CENTER, NORTH, EAST, and so on. The JLabet class is one of several Swing classes that implement this interface. Therefore, you can specify a right-aligned label either as
var label = new JLabel(“User name: “, SwingConstants.RIGHT);
or
var label = new JLabel(“User name: “, JLabel.RIGHT);
The setText and setIcon methods let you set the text and icon of the label at runtime.
Labels can be positioned inside a container like any other component. This means you can use the techniques you have seen before to place your labels where you need them.
3. Password Fields
Password fields are a special kind of text fields. To prevent nosy bystanders from seeing your password, the characters that the user enters are not actually displayed. Instead, each typed character is represented by an echo character, such as a bullet (•). Swing supplies a JPasswordFietd class that implements such a text field.
The password field is another example of the power of the model-view- controller architecture pattern. The password field uses the same model to store the data as a regular text field, but its view has been changed to display all characters as echo characters.
4. Text Areas
Sometimes, you need to collect user input that is more than one line long. As mentioned earlier, you can use the JTextArea component for this. When you place a text area component in your program, a user can enter any number of lines of text, using the Enter key to separate them. Each line ends with a ‘\n’. Figure 11.11 shows a text area at work.
In the constructor for the JTextArea component, specify the number of rows and columns for the text area. For example,
textArea = new JTextArea(8, 40); // 8 lines of 40 columns each
where the columns parameter works as before—and you still need to add a few more columns for safety’s sake. Also, as before, the user is not restricted to the number of rows and columns; the text simply scrolls when the user inputs too much. You can also use the setCotumns method to change the number of columns and the setRows method to change the number of rows. These numbers only indicate the preferred size—the layout manager can still grow or shrink the text area.
If there is more text than the text area can display, the remaining text is simply clipped. You can avoid clipping long lines by turning on line wrapping:
textArea.setLineWrap(true); // tong tines are wrapped
This wrapping is a visual effect only; the text in the document is not changed—no automatic ‘\n’ characters are inserted into the text.
5. Scroll Panes
In Swing, a text area does not have scrollbars. If you want scrollbars, you have to place the text area inside a scroll pane.
textArea = new JTextArea(8, 40);
var scrottPane = new JScroUPane(textArea);
The scroll pane now manages the view of the text area. Scrollbars automatically appear if there is more text than the text area can display, and they vanish again if text is deleted and the remaining text fits inside the area. The scrolling is handled internally by the scroll pane—your program does not need to process scroll events.
This is a general mechanism that works for any component, not just text areas. To add scrollbars to a component, put them inside a scroll pane.
Listing 11.1 demonstrates the various text components. This program shows a text field, a password field, and a text area with scrollbars. The text field and password field are labeled. Click on “Insert” to insert the field contents into the text area.
Source: Horstmann Cay S. (2019), Core Java. Volume I – Fundamentals, Pearson; 11th edition.