(Translation) 14th back javafx2.0 text box textfield

Source: Internet
Author: User

Original address http://download.oracle.com/javafx/2.0/ui_controls/text-field.htm

 

The textfield class implements a UI control that can accept and display text input. It provides the function of accepting user input. And another text input controlPasswordfield inherited togetherTextinput class,Textinput is the parent class of all text controls.

 

Figure 8-1 is a typical text box with labels.

Figure 8-1 label and text field


Description of "Figure 8-1 label and text field"

Create text field

In Example 8-1, a text box and a label are used to display the input content type.

Example 8-1 Creating a text field

Label label1 = new Label("Name:");TextField textField = new TextField ();HBox hb = new HBox();hb.getChildren().addAll(label1, textField);hb.setSpacing(10);

You can create an empty text box or a text box with a specific text data as in Example 8-1. To create a text box with predefined text, use the following textfield class constructor:Textfield ("Hello
World! "). You can use
getTextMethod to obtain the value of a text box.

 

AvailableTextInputClassThe setprefcolumncount method sets the size of the text box and defines the maximum number of characters displayed in the text box at a time.

Build UI with text field

Generally,Textfield object is used to create several text boxes.Figure
The application in 8-2 displays three text boxes and processes the data entered by users.

Figure 8-2 textfieldsample Application


Description of "Figure 8-2 textfieldsample application"

The code block in Example 8-2 creates three text boxes and two buttonsGridPaneContainer they are added to the application screen. This container is quite convenient to implement flexible layout for your UI controls.

Example 8-2 adding text fields to the application

//Creating a GridPane containerGridPane grid = new GridPane();grid.setPadding(new Insets(10, 10, 10, 10));grid.setVgap(5);grid.setHgap(5);//Defining the Name text fieldfinal TextField name = new TextField();name.setPromptText("Enter your first name.");name.setPrefColumnCount(10);name.getText();GridPane.setConstraints(name, 0, 0);grid.getChildren().add(name);//Defining the Last Name text fieldfinal TextField lastName = new TextField();lastName.setPromptText("Enter your last name.");GridPane.setConstraints(lastName, 0, 1);grid.getChildren().add(lastName);//Defining the Comment text fieldfinal TextField comment = new TextField();comment.setPrefColumnCount(15);comment.setPromptText("Enter your comment.");GridPane.setConstraints(comment, 0, 2);grid.getChildren().add(comment);//Defining the Submit buttonButton submit = new Button("Submit");GridPane.setConstraints(submit, 1, 0);grid.getChildren().add(submit);//Defining the Clear buttonButton clear = new Button("Clear");GridPane.setConstraints(clear, 1, 1);grid.getChildren().add(clear);

Take some time to study this code.name,lastName, AndThe comment text box is used.TextFieldClass empty constructor to create. And example
8-1 is different. Here, the text box does not use labels, but uses prompts to remind users of the type of data to be entered in the text box.The setprompttext method defines the strings displayed in the text box after the application is started. SetExample
The code in 8-2 is added to the application. The running effect is shown in Figure 8-3.

 

Figure 8-3 three text fields with the prompt messages


Description of "Figure 8-3 three text fields with the prompt messages"

The difference between the prompt and text in the text box is that the prompt cannot passGet the gettext method.

In practice, the text entered in the text box is processed based on the application logic determined by a specific business task. The next section explains how to use text boxes to process user input and provide feedback to users.

Process text field data

As mentioned above, you can useTextInputClassGet the gettext method.

Study how to learn the code in Example 8-3Textfield object data.

Example 8-3 defining actions for the submit and clear buttons

//Adding a Labelfinal Label label = new Label();GridPane.setConstraints(label, 0, 3);GridPane.setColumnSpan(label, 2);grid.getChildren().add(label);//Setting an action for the Submit buttonsubmit.setOnAction(new EventHandler<ActionEvent>() {@Override    public void handle(ActionEvent e) {        if ((comment.getText() != null && !comment.getText().isEmpty())) {            label.setText(name.getText() + " " + lastName.getText() + ", "                + "thank you for your comment!");        } else {            label.setText("You have not left a comment.");        }     } }); //Setting an action for the Clear buttonclear.setOnAction(new EventHandler<ActionEvent>() {@Override    public void handle(ActionEvent e) {        name.setText("");        lastName.setText("");        comment.setText("");        label.setText(null);    }}); 

In the gridpane containerThe label control is used to display the application's response to the user. When a user clicksWhen the submit button is used,Setonaction method checkComment text box. If it is a non-null string, a thank-you message is displayed. Otherwise, the app will remind the user that no comments have been added.See figure
8-4.

Figure 8-4 The comment text field left blank


Description of "Figure 8-4 The comment text field left blank"

When you click the Clear button, the content of the three text boxes will be cleared.

. Review the text box functions you may use.

  • copy()-Transfer the selected text range to the clipboard and retain the selected text.

  • cut()-Transfer the selected text range to the clipboard and delete the selected text.

  • paste()-Transfer the clipboard content to the text to replace the selected text.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.