JavaFX GUI
Building modern desktop applications with rich user interfaces
🎨 What is JavaFX?
JavaFX is a modern Java framework for building rich desktop applications with beautiful user interfaces. It provides powerful graphics, media, and web capabilities for creating engaging desktop experiences.
// Simple JavaFX Application
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class HelloFX extends Application {
public void start(Stage stage) {
Label label = new Label("Hello JavaFX!");
Scene scene = new Scene(label, 300, 200);
stage.setScene(scene);
stage.setTitle("My First JavaFX App");
stage.show();
}
}
Output:
Hello JavaFX!
Key JavaFX Concepts
Stage & Scene
Window container and content area
Stage stage = new Stage();
Scene scene = new Scene(root, 400, 300);
Controls
UI components like buttons, labels
Button btn = new Button("Click Me");
Label lbl = new Label("Welcome");
Layouts
Organize components in containers
VBox vbox = new VBox();
HBox hbox = new HBox();
GridPane grid = new GridPane();
Events
Handle user interactions
btn.setOnAction(e ->
System.out.println("Clicked!"));
🔹 Basic JavaFX Application Structure
Every JavaFX application extends Application class:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class MyApp extends Application {
@Override
public void start(Stage primaryStage) {
// Create UI components
Button button = new Button("Say Hello");
button.setOnAction(e -> System.out.println("Hello World!"));
// Create layout
VBox root = new VBox();
root.getChildren().add(button);
// Create scene and show stage
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("JavaFX Example");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
🔹 Common JavaFX Controls
Essential UI components for building interfaces:
// Text controls
Label label = new Label("Enter your name:");
TextField textField = new TextField();
TextArea textArea = new TextArea();
// Buttons
Button button = new Button("Submit");
CheckBox checkBox = new CheckBox("Remember me");
RadioButton radio = new RadioButton("Option 1");
// Lists and choices
ListView<String> listView = new ListView<>();
ComboBox<String> comboBox = new ComboBox<>();
// Event handling
button.setOnAction(event -> {
String name = textField.getText();
label.setText("Hello, " + name + "!");
});
🔹 Layout Containers
Organize your UI components with different layouts:
// Vertical layout
VBox vbox = new VBox(10); // 10px spacing
vbox.getChildren().addAll(label, textField, button);
// Horizontal layout
HBox hbox = new HBox(5);
hbox.getChildren().addAll(button1, button2, button3);
// Grid layout
GridPane grid = new GridPane();
grid.add(new Label("Name:"), 0, 0);
grid.add(new TextField(), 1, 0);
grid.add(new Label("Email:"), 0, 1);
grid.add(new TextField(), 1, 1);
// Border layout
BorderPane border = new BorderPane();
border.setTop(menuBar);
border.setCenter(content);
border.setBottom(statusBar);