TextField 정의하기

In Step 25 of the exercise, as you entered text into the TextField shown in Figure 6-9, the contents of the TextField were printed to the Java console each time the contents changed (e.g., as characters were typed into the TextField). The following snippet from Listing 6-12 creates the TextField and implements these behaviors:

final TextField textField = new TextField();

textField.setPromptText("Enter user name");
textField.setPrefColumnCount(16);
textField.textProperty().addListener((ov, oldValue, newValue) -> {
    System.out.println("TextField text is: " + textField.getText());
});

To detect when the text property of the TextField has changed, the code in this snippet adds a ChangeListener to the text property. The new value of the text property is then printed to the Java console in the body of the lambda expression.

Using a PasswordField

The PasswordField extends the TextField class, and its purpose is to mask the characters that are typed into it. In Step 26 of the exercise, when you entered text into the PasswordField shown in Figure 6-9 and subsequently caused the PasswordField to lose focus, the contents of the PasswordField were printed to the Java console. The following snippet from Listing 6-12 creates the PasswordField and implements these behaviors:

final PasswordField passwordField = new PasswordField();
    passwordField.setPromptText("Enter password");
    passwordField.setPrefColumnCount(16);
    passwordField.focusedProperty().addListener((ov, oldValue, newValue) -> {
        if (!passwordField.isFocused()) {
            System.out.println("PasswordField text is: "
                    + passwordField.getText());
        }
    });

To detect when the PasswordField has lost focus, the code in the preceding snippet adds a ChangeListener to the focused property. The value of the text property is then printed to the Java console in the body of the lambda expression if the PasswordField is indeed not focused.

Creating a TextArea

The TextArea control is similar to the TextField control, but allows for multiple lines of text. In Step 27 of the exercise, when you entered text into the TextArea shown in Figure 6-9 and subsequently caused the TextArea to lose focus, the contents of the TextArea were printed to the Java console. The following snippet from Listing 6-12 creates the TextArea and implements these behaviors:

final TextArea textArea = new TextArea();
    textArea.setPrefColumnCount(12);
    textArea.setPrefRowCount(4);
    textArea.focusedProperty().addListener((ov, oldValue, newValue) -> {
        if (!textArea.isFocused()) {
            System.out.println("TextArea text is: " + textArea.getText());
        }
    });

Some useful TextArea properties not demonstrated in the preceding snippet are: wrapText, which controls whether the text will wrap in the TextArea scrollLeft/scrollTop, which are the number of pixels by which the content is horizontally or vertically scrolled Let’s move away from the UI controls that accept text input, and briefly talk about two very convenient utilities.