TextField
Although the controls
discussed earlier are quite useful and are frequently found in a user
interface, they all implement a means of selecting a predetermined option or
action. However, sometimes you will want the user to enter a string of his or
her own choosing. To accommodate this type of input, JavaFX includes several
text-based controls. The one we will look at is TextField. It allows one line of text to be entered. Thus, it is
useful for obtaining names, ID strings, addresses, and the like. Like all text
controls, TextField inherits TextInputControl, which defines much of
its functionality.
TextField defines two constructors. The first is the default constructor,
which creates an empty text field
that has the default size. The second lets you specify the initial contents of
the field. Here, we will use the default constructor.
Although the default size is
sometimes adequate, often you will want to specify its size. This is done by
calling setPrefColumnCount( ), shown
here:
final void
setPrefColumnCount(int columns)
The columns value is used by TextField
to determine its size.
You can set the text in a
text field by calling setText( ).
You can obtain the current text by calling getText(
). In addition to these fundamental operations, TextField supports several other capabilities that you might want
to explore, such as cut, paste, and append. You can also select a portion of
the text under program control.
One especially useful TextField option is the ability to set
a prompting message inside the text field when the user attempts to use a blank
field. To do this, call setPromptText( ),
shown here:
final void setPromptText(String
str)
In this case, str is the string displayed in the text
field when no text has been entered. It is displayed using low-intensity (such
as a gray tone).
When the user presses enter
while inside a TextField, an action
event is generated. Although handling this event is often helpful, in some
cases, your program will simply obtain the text when it is needed, rather than
handling action events. Both approaches are demonstrated by the following
program. It creates a text field that requests a search string. When the user
presses enter while the text field has input focus, or presses the Get Search
String button, the string is obtained and displayed. Notice that a prompting
message is also included.
// Demonstrate a text field.
import javafx.application.*; import
javafx.scene.*; import javafx.stage.*; import javafx.scene.layout.*; import
javafx.scene.control.*; import javafx.event.*;
import javafx.geometry.*;
public class TextFieldDemo extends Application
{
TextField tf;
Label response;
public static void main(String[] args) {
// Start the JavaFX application by calling
launch().
Launch(args);
}
// Override the start() method.
public void start(Stage myStage) {
//Give the stage a title.
myStage.setTitle("Demonstrate a
TextField");
//Use a FlowPane for the root node. In this
case,
//vertical and horizontal gaps of 10.
FlowPane rootNode = new FlowPane(10, 10);
//Center the controls in the scene.
rootNode.setAlignment(Pos.CENTER);
//Create a scene.
Scene myScene = new Scene(rootNode, 230, 140);
//Set the scene on the stage.
myStage.setScene(myScene);
//Create a label that will report the contents
of the
//text field.
response = new Label("Search String:
");
// Create a button that gets the text.
Button btnGetText = new Button("Get Search
String");
//Create a text field.
tf = new TextField();
//Set the prompt.
tf.setPromptText("Enter Search
String");
//Set preferred column count.
tf.setPrefColumnCount(15);
//Handle action events for the text field.
Action
//events are generated when ENTER is pressed
while
//the text field has input focus. In this case,
the
//text in the field is obtained and displayed.
tf.setOnAction(new
EventHandler<ActionEvent>() {
public void handle(ActionEvent ae) {
response.setText("Search String: " + tf.getText());
}
});
//Get text from the text field when the button
is pressed
//and display it.
btnGetText.setOnAction(new
EventHandler<ActionEvent>() { public void handle(ActionEvent ae) {
response.setText("Search String: " +
tf.getText());
}
});
//Use a separator to better organize the
layout.
Separator separator = new Separator();
separator.setPrefWidth(180);
//Add controls to the scene graph.
rootNode.getChildren().addAll(tf, btnGetText,
separator, response);
// Show the stage and its scene.
myStage.show();
}
}
Sample output is shown here:
Other text controls that you
will want to examine include TextArea,
which supports multiline text, and PasswordField,
which can be used to input passwords. You might also find HTMLEditor helpful.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.