Basic Controls in JavaFX - dummies

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 lbl = new Label("This is a label");


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


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


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");
ToggleGroup options = new ToggleGroup();
options.getToggles().addAll(rdo1, rdo2);
if (rdo1.isSelected())
System.out.println("Option 1 is selected");