javafx


๐Ÿงพ JavaFX


๐Ÿ“ฆ 1. Setup

Add JavaFX to your project

For Maven, add dependencies:

<dependencies>
  <dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>XX</version>
  </dependency>
  <!-- Add more modules if needed -->
</dependencies>

๐Ÿš€ 2. Launching a JavaFX App

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class MainApp extends Application {
    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("Hello JavaFX!");
        Scene scene = new Scene(label, 300, 200);
        primaryStage.setTitle("My JavaFX App");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

๐Ÿงฑ 3. Layouts

Common Layout Panes

HBox hbox = new HBox(10);        // Horizontal layout
VBox vbox = new VBox(10);        // Vertical layout
BorderPane border = new BorderPane(); // Top, Bottom, Left, Right, Center
GridPane grid = new GridPane();  // Row/column layout
StackPane stack = new StackPane(); // Stack nodes
FlowPane flow = new FlowPane();  // Flow layout

๐Ÿงฉ 4. Controls (UI Elements)

Control Example
Button Button btn = new Button("Click");
Label Label lbl = new Label("Text");
TextField TextField tf = new TextField();
PasswordField PasswordField pf = new PasswordField();
TextArea TextArea ta = new TextArea();
CheckBox CheckBox cb = new CheckBox("Yes");
RadioButton RadioButton rb = new RadioButton();
ComboBox ComboBox<String> cb = new ComboBox<>();
ListView ListView<String> lv = new ListView<>();
TableView TableView<Person> table = new TableView<>();

๐Ÿ–ฑ๏ธ 5. Event Handling

Button Click Example

btn.setOnAction(e -> {
    System.out.println("Button clicked!");
});

Text Change Listener

textField.textProperty().addListener((obs, oldText, newText) -> {
    System.out.println("Changed to: " + newText);
});

๐ŸŽจ 6. Styling (CSS)

Inline Style

button.setStyle("-fx-background-color: #00FF00;");

External CSS

/* style.css */
.button {
    -fx-background-color: #336699;
    -fx-text-fill: white;
}

Applying CSS

scene.getStylesheets().add("style.css");

๐Ÿ“„ 7. FXML (Optional UI Markup)

FXML File (sample.fxml)

<Button fx:id="myButton" text="Click Me" onAction="#handleClick" />

Controller

public class MyController {
    @FXML private Button myButton;

    @FXML private void handleClick(ActionEvent event) {
        System.out.println("Clicked!");
    }
}

Load FXML

FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = loader.load();

๐Ÿ“š 8. Useful Tips


Would you like this cheat sheet as a downloadable PDF or image? Or should I include advanced topics like animations, charts, or custom components?

Absolutely! Here's a focused and clear JavaFX Observable Cheat Sheet, covering the core Observable* types used in JavaFX for building reactive UIs.


๐Ÿ“˜ JavaFX Observable Cheat Sheet


๐Ÿง  What is Observable in JavaFX?

In JavaFX, an Observable is an object that allows listeners to observe and react to changes in its value or contents. It forms the basis of JavaFX's reactive property system.


๐Ÿงฉ Core Observable Interfaces

Interface Description
Observable Marker interface for observables.
ObservableValue<T> A read-only observable value.
WritableValue<T> A value you can read and write.
ObservableList<T> A list that notifies listeners of changes.
ObservableMap<K, V> A map with observable key-value changes.
ObservableSet<E> A set that allows observation of element changes.

๐Ÿ” Observable Properties

These are commonly used JavaFX bindings that wrap data and support listeners:

Primitive Properties

StringProperty name = new SimpleStringProperty("John");
IntegerProperty age = new SimpleIntegerProperty(25);
DoubleProperty salary = new SimpleDoubleProperty(50000.0);
BooleanProperty active = new SimpleBooleanProperty(true);

Object Property

ObjectProperty<LocalDate> date = new SimpleObjectProperty<>(LocalDate.now());

๐Ÿ“ก Listening to Changes

Change Listener

name.addListener((obs, oldVal, newVal) -> {
    System.out.println("Changed from " + oldVal + " to " + newVal);
});

Invalidation Listener

name.addListener((obs) -> {
    System.out.println("Property was invalidated!");
});

๐Ÿ”— Binding Properties

Unidirectional Binding

label.textProperty().bind(name);  // Label updates when name changes

Bidirectional Binding

textField.textProperty().bindBidirectional(name);

Binding with Expression

total.bind(price.multiply(quantity));  // total = price ร— quantity

๐Ÿงฎ Common Binding Utilities

Bindings.concat(str1, str2)
Bindings.when(condition).then(val1).otherwise(val2)
Bindings.add(a, b)
Bindings.subtract(a, b)
Bindings.createStringBinding(() -> customLogic(), dependencies...)

๐Ÿ—‚๏ธ Collections with Observables

ObservableList

ObservableList<String> list = FXCollections.observableArrayList();

ObservableMap / ObservableSet

ObservableMap<String, String> map = FXCollections.observableHashMap();
ObservableSet<String> set = FXCollections.observableSet();

Listeners

list.addListener((ListChangeListener<String>) change -> { /* ... */ });
map.addListener((MapChangeListener<String, String>) change -> { /* ... */ });
set.addListener((SetChangeListener<String>) change -> { /* ... */ });

โš ๏ธ Watch Out For


Would you like a diagram of property relationships or a PDF export of this cheat sheet? I can also extend this with custom binding examples, animation observables, or best practices.


Q: JavaFX ObservableList Cheat Sheet

Absolutely! Here's a concise yet complete JavaFX ObservableList Cheat Sheet, perfect for quick reference when working with dynamic data-bound UI elements like ListView, TableView, and more.


๐Ÿ“˜ JavaFX ObservableList Cheat Sheet


๐Ÿง  What is ObservableList?

ObservableList is a special list in JavaFX that notifies listeners when elements are added, removed, or modified. It's commonly used in UI controls like ListView, ComboBox, TableView, etc.


๐Ÿ—๏ธ Creating an ObservableList

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

// Empty list
ObservableList<String> list = FXCollections.observableArrayList();

// With initial elements
ObservableList<String> list2 = FXCollections.observableArrayList("A", "B", "C");

// From existing collection
List<String> baseList = Arrays.asList("X", "Y");
ObservableList<String> observable = FXCollections.observableList(baseList);

๐Ÿ” Basic Operations

list.add("New Item");
list.addAll("One", "Two");
list.remove("Two");
list.remove(0);           // remove by index
list.set(0, "Updated");   // replace
list.clear();

๐Ÿ” Accessing Elements

String item = list.get(0);
int size = list.size();
boolean contains = list.contains("X");

๐Ÿ”” Listening for Changes

list.addListener((ListChangeListener<String>) change -> {
    while (change.next()) {
        if (change.wasAdded()) {
            System.out.println("Items added: " + change.getAddedSubList());
        }
        if (change.wasRemoved()) {
            System.out.println("Items removed: " + change.getRemoved());
        }
    }
});

๐Ÿ”— Binding to UI Controls

ListView<String> listView = new ListView<>();
listView.setItems(list);  // Automatically updates when list changes

ComboBox<String> comboBox = new ComboBox<>();
comboBox.setItems(list);

๐Ÿงฉ Filtering and Sorting (ObservableList View)

import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;

// Filtered
FilteredList<String> filtered = new FilteredList<>(list, s -> s.startsWith("A"));

// Sorted
SortedList<String> sorted = new SortedList<>(list);
sorted.setComparator((a, b) -> a.compareToIgnoreCase(b));

// Bind to a TableView or ListView
listView.setItems(filtered);

โš™๏ธ Advanced: Custom Object Lists

ObservableList<Person> people = FXCollections.observableArrayList(
    person -> new Observable[] { person.nameProperty(), person.ageProperty() }
);

Person Class Example

public class Person {
    private final StringProperty name = new SimpleStringProperty();
    private final IntegerProperty age = new SimpleIntegerProperty();

    // Getters & Setters
    public StringProperty nameProperty() { return name; }
    public IntegerProperty ageProperty() { return age; }
}

๐Ÿ’ก Tips


Would you like this cheat sheet exported to a PDF, or should I include usage examples with TableView or custom cell factories too?