Cheat Sheet

JavaFX For Dummies Cheat Sheet

From JavaFX For Dummies

By Doug Lowe

The trick to developing JavaFX applications is negotiating your way through the plethora of classes that make up the JavaFX API. At the minimum, you’ll need to use the Scene and Stage classes in your application’s start method. And you’ll need to use basic layout classes, control classes, and shape classes.

Getting Started with JavaFX

The start method is the heart of any JavaFX application. This method is called when the application is loaded, and a reference to the application’s primary Stage is passed as a parameter. The start method creates the Scene object and displays it in the stage.

@Override public void start(Stage primaryStage)
{
    Group root = new Group(); 
    Scene scene = new Scene(root, 300, 250);
    primaryStage.setScene(scene);
    primaryStage.setTitle("The Click Me App");
    primaryStage.show();
}

Basic Layout Panes in JavaFX

Layout panes in JavaFX let you manage the placement controls and shapes that appear in your scenes. Don’t forget that a layout pane is itself a node, so you can nest layout panes within other layout panes to create complicated layouts.

HBox

HBox hbox1 = new HBox(10);     // sets spacing
hbox1.getChildren().addAll(btn1, btn2, btn3);
hbox1.setAlignment(Pos.CENTER);
hbox1.setPadding(new Insets(10));
hbox1.setSpacing(10);

VBox

VBox vbox1 = new VBox(10);     // sets spacing
vbox1.getChildren().addAll(btn1, btn2, btn3);
vbox1.setAlignment(Pos.CENTER);
vbox1.setPadding(new Insets(10));
vbox1.setSpacing(10);

FlowPane

FlowPane flow1 = new FlowPane(10, 10);
flow1.getChildren().addAll(btn1, btn2, btn3);
flow1.setAlignment(Pos.CENTER);
flow1.setOrientation(Orientation.VERTICAL);
flow1.setVgap(10);
flow1.setHgap(10);
flow1.setPrefWrapLength(5);

BorderPane

BorderPane border1 = new BorderPane();
border1.setCenter(btn1);
border1.setTop(btn2);
border1.setRight(btn3);
border1.setBottom(btn4);
border1.setLeft(btn5);

Basic Controls in JavaFX

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");

Basic Shapes in JavaFX

With JavaFX, you can draw basic two-dimensional objects such as lines, arcs, rectangles, ellipses. You can also fill the shape with a solid color, a gradient fill, or text that’s created from an image. You can make your shapes solid or transparent, and you can rotate, clip, skew, and do all sorts of other unspeakable things to them.

Shape Constructors

Rectangle rect1 = new Rectangle(100, 100, 200, 200);
Circle circle1 = new Circle(100, 100, 200);
Ellipse ellipse1 = new Ellipse(100, 100, 200, 250);
Line line1 = new Line(100,100,200,200);

Fill and Stroke

Shape1.setFill(Color.RED);
shape1.setStroke(Color.BLACK);
shape1.setStrokeWidth(3);

Translate and Rotate

Shape1.setTranslateX(100);
shape1.setTranslateY(100);
shape1.setRotate(90);