So far, the only input we've been able to get from the user is whether or not they have clicked on a button. While this does have many uses (infinitely expandable via event handlers), we may want to gather other types of information from the user. In this section, we will look at three types of user input elements: text fields, check boxes, and radio buttons.
javafx.scene.control.TextField
TextField
s allow the user to input arbitrary text to be parsed and used by the program. This text can potentially include alphabetic characters, digits, logographs, abjadi letters, or more, and it is up to the programmer to parse the correct data. Luckily, the text is stored as a simple String
, allowing us to manipulate it easily.
Method | Description |
---|---|
TextField() |
Creates a new, empty TextField |
TextField(String) |
Creates a new TextField with some initial text |
String getText() |
Gets the text entered into the field |
void requestFocus() |
Asks for the cursor to be set on this field |
void setEditable(boolean) |
If false , the field is read-only |
void setMaxWidth(double) |
Set the maximum width of the field |
void setMinWidth(double) |
Set the minimum width of the field |
void setPromptText(String) |
Set the prompt text, the text displayed when not in focus or without entered text |
void setText(String) |
Set the text entered into the field |
javafx.scene.control.CheckBox
CheckBox
es allow the user to select and deselect potentially multiple options from a group.
Method | Description |
---|---|
CheckBox() |
Creates a new, empty, deselected CheckBox |
CheckBox(String) |
Creates a new, deselected CheckBox with the specified text |
String getText() |
Gets the text displayed by the CheckBox |
boolean isSelected() |
true if the box is selected, false otherwise |
void setOnAction(EventHandler<ActionEvent>) |
Sets the event handler for the CheckBox |
void setSelected(boolean) |
Selects the box if true , deselects if false |
void setText(String) |
Sets the text displayed by the box |
javafx.scene.control.RadioButton
RadioButton
s allow the user to pick one from a group of options. They are separated into different ToggleGroup
classes, which define these groups.
Method | Description |
---|---|
RadioButton() |
Create a new, empty, deselected RadioButton |
RadioButton(String) |
Create a new, deselected RadioButton with the specified label |
ToggleGroup() |
Create a new, empty ToggleGroup |
String getText() |
Get the text of the RadioButton |
boolean isSelected() |
true if the box is selected, false otherwise |
void setOnAction(EventHandler<ActionEvent>) |
Sets the event handler for the RadioButton |
void setSelected(boolean) |
Selects the box if true , deselects if false |
void setText(String) |
Sets the text displayed by the box |
void setToggleGroup(ToggleGroup) |
Sets the ToggleGroup of the button |
To see how each of these part work, we're going to create an application to order a pizza. The program will take in the following information:
Finally, after, confirming, the application will display the complete order back to the user.
package edu.govschool;
import javafx.application.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.*;
import javafx.stage.*;
/**
* Demonstrates the use of <code>TextField</code>s, <code>CheckBox</code>es, and
* <code>RadioButton</code>s.
* @author Mr. Davis
*/
public class PizzaOrder extends Application {
// Our main stage
private static Stage stage;
// Customer information
private static TextField txtName;
private static TextField txtPhone;
private static TextField txtAddress;
// Pizza size options
private static RadioButton rdoSmall;
private static RadioButton rdoMedium;
private static RadioButton rdoLarge;
// Crust style options
private static RadioButton rdoThin;
private static RadioButton rdoThick;
// Toppings options
private static CheckBox chkPepperoni;
private static CheckBox chkSausage;
private static CheckBox chkOlives;
private static CheckBox chkMushrooms;
private static CheckBox chkTomatoes;
private static CheckBox chkAnchovies;
@Override
public void start(Stage primaryStage) {
// Set our main stage
stage = primaryStage;
// Create the title pane
Text textHeading = new Text("BESTELL 'NE PIZZA JETZT");
textHeading.setFont(new Font(20));
HBox paneTop = new HBox(textHeading);
paneTop.setPadding(new Insets(20, 10, 20, 10));
/* Create the customer pane */
// Name
Label lblName = new Label("Name:");
// setPrefWidth(double) sets the default width of the field.
lblName.setPrefWidth(100);
txtName = new TextField();
// setPrefColumnCount(int) sets the number of characters to display at a
// time.
txtName.setPrefColumnCount(20);
txtName.setPromptText("Enter the customer's name");
txtName.setMaxWidth(Double.MAX_VALUE);
HBox paneName = new HBox(lblName, txtName);
// Phone Number
Label lblPhone = new Label("Phone:");
lblPhone.setPrefWidth(100);
txtPhone = new TextField();
txtPhone.setPrefColumnCount(20);
txtPhone.setPromptText("Enter the customer's phone number");
HBox panePhone = new HBox(lblPhone, txtPhone);
// Address
Label lblAddress = new Label("Address:");
lblAddress.setPrefWidth(100);
txtAddress = new TextField();
txtAddress.setPrefColumnCount(20);
txtAddress.setPromptText("Enter the customer's address");
HBox paneAddress = new HBox(lblAddress, txtAddress);
VBox paneCustomer = new VBox(10, paneName, panePhone, paneAddress);
/* Create the order pane */
// Pizza size
Label lblSize = new Label("Size");
rdoSmall = new RadioButton("Small");
rdoMedium = new RadioButton("Medium");
rdoLarge = new RadioButton("Large");
rdoMedium.setSelected(true);
ToggleGroup groupSize = new ToggleGroup();
rdoSmall.setToggleGroup(groupSize);
rdoMedium.setToggleGroup(groupSize);
rdoLarge.setToggleGroup(groupSize);
VBox paneSize = new VBox(lblSize, rdoSmall, rdoMedium, rdoLarge);
paneSize.setSpacing(10);
// Crust style
Label lblCrust = new Label("Crust");
rdoThin = new RadioButton("Thin");
rdoThick = new RadioButton("Thick");
rdoThin.setSelected(true);
ToggleGroup groupCrust = new ToggleGroup();
rdoThin.setToggleGroup(groupCrust);
rdoThick.setToggleGroup(groupCrust);
VBox paneCrust = new VBox(lblCrust, rdoThin, rdoThick);
paneCrust.setSpacing(10);
// Toppings
Label lblToppings = new Label("Toppings");
chkPepperoni = new CheckBox("Pepperoni");
chkSausage = new CheckBox("Sausage");
chkOlives = new CheckBox("Olives");
chkMushrooms = new CheckBox("Mushrooms");
chkTomatoes = new CheckBox("Tomatoes");
chkAnchovies = new CheckBox("Anchovies");
FlowPane paneToppings = new FlowPane(Orientation.VERTICAL,
chkPepperoni, chkSausage, chkOlives, chkMushrooms,
chkTomatoes, chkAnchovies);
paneToppings.setPadding(new Insets(10, 0, 10, 0));
paneToppings.setHgap(20);
paneToppings.setVgap(10);
paneToppings.setPrefWrapLength(100);
VBox paneTopping = new VBox(lblToppings, paneToppings);
// Add the sub-panes to the order pane
HBox paneOrder = new HBox(50, paneSize, paneCrust, paneTopping);
// Create the center pane
VBox paneCenter = new VBox(20, paneCustomer, paneOrder);
paneCenter.setPadding(new Insets(0, 10, 0, 10));
/* Create the bottom pane */
Button btnOK = new Button("OK");
btnOK.setPrefWidth(80);
btnOK.setOnAction(e -> btnOK_click());
Button btnCancel = new Button("Cancel");
btnCancel.setPrefWidth(80);
btnCancel.setOnAction(e -> btnCancel_click());
Region spacer = new Region();
HBox paneBottom = new HBox(10, spacer, btnOK, btnCancel);
HBox.setHgrow(spacer, Priority.ALWAYS);
paneBottom.setPadding(new Insets(20, 10, 20, 10));
/* Finish the scene */
BorderPane paneMain = new BorderPane();
paneMain.setTop(paneTop);
paneMain.setCenter(paneCenter);
paneMain.setBottom(paneBottom);
Scene scene = new Scene(paneMain);
stage.setScene(scene);
stage.setTitle("Pizza Order");
stage.show();
}
public void btnOK_click()
{
// Create our message string
String msg = "Customer:\n\n";
msg += "\t" + txtName.getText() + "\n";
msg += "\t" + txtAddress.getText() + "\n";
msg += "\t" + txtPhone.getText() + "\n";
msg += "You have ordered a ";
if (rdoSmall.isSelected()) {
msg += "small ";
} else if (rdoMedium.isSelected()) {
msg += "medium ";
} else if (rdoLarge.isSelected()) {
msg += "large ";
}
if (rdoThin.isSelected()) {
msg += "thin crust pizza with ";
} else if (rdoThick.isSelected()) {
msg += "thick crust pizza with ";
}
String toppings = "";
toppings = buildToppings(chkPepperoni, toppings);
toppings = buildToppings(chkSausage, toppings);
toppings = buildToppings(chkOlives, toppings);
toppings = buildToppings(chkTomatoes, toppings);
toppings = buildToppings(chkMushrooms, toppings);
toppings = buildToppings(chkAnchovies, toppings);
if (toppings.equals("")) msg += "no toppings.";
else msg += "the following toppings:\n" + toppings;
MessageBox.show(msg, "Order Details");
}
public String buildToppings(CheckBox chk, String msg)
{
if (chk.isSelected()) {
if (!msg.equals("")) msg += ", ";
msg += chk.getText();
}
return msg;
}
public void btnCancel_click()
{
stage.close();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}