Basic Controls in JavaFX

By Doug Lowe

Part of JavaFX For Dummies Cheat Sheet

JavaFX defines a plethora of controls that you use to interact with the user. The most commonly used are Label, Button, TextField, CheckBox, and RadioButton.

Label

Label lbl = new Label("This is a label");

Button

Button btn = new Button("Click Me!");
btn.setOnAction(e -> System.out.println("Click"));

TextField

TextField txtInput = new TextField("Prompt Text");
String input = txtInput.getText();

CheckBox

CheckBox chkOption = new CheckBox("Check Box Text");
if (chkOption.isSelected())
System.out.println("Option is selected");

RadioButton (with ToggleGroup)

RadioButton rdo1 = new RadioButton("Option 1");
RadioButton rdo2 = new RadioButton("Option 2");
rdo1.setSelected(true);
ToggleGroup options = new ToggleGroup();
options.getToggles().addAll(rdo1, rdo2);
if (rdo1.isSelected())
System.out.println("Option 1 is selected");