JavaFX is a set of graphics and media packages that enable developers to design, create, test, debug, and deploy rich client applications that operate consistently across diverse platforms. Here’s a cheat sheet for JavaFX tasks and concepts:
Basic Structure
JavaFX Application Class:
public class MyJavaFXApp extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Application code
}
}
UI Components
Button:
Button button = new Button("Click me");
Label:
Label label = new Label("Hello, JavaFX!");
TextField:
TextField textField = new TextField();
ComboBox:
ComboBox<String> comboBox = new ComboBox<>();
Layouts
VBox (Vertical Box):
VBox vbox = new VBox();
HBox (Horizontal Box):
HBox hbox = new HBox();
BorderPane:
BorderPane borderPane = new BorderPane();
Event Handling
Button Click Event:
button.setOnAction(e -> {
System.out.println("Button clicked!");
});
Scene and Stage
Create Scene:
Scene scene = new Scene(root, width, height);
Set Scene to Stage:
primaryStage.setScene(scene);
CSS Styling
Add Stylesheet:
scene.getStylesheets().add("styles.css");
Apply Style Class:
button.getStyleClass().add("my-button");
FXML
Load FXML File:
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = loader.load();
Media and Images
Load Image:
Image image = new Image("image.png");
ImageView:
ImageView imageView = new ImageView(image);
Animations
Translate Transition:
TranslateTransition transition = new TranslateTransition(Duration.seconds(1), node);
transition.setToX(100);
transition.play();
Charts
BarChart:
BarChart<String, Number> barChart = new BarChart<>(xAxis, yAxis);
LineChart:
LineChart<String, Number> lineChart = new LineChart<>(xAxis, yAxis);
This JavaFX cheat sheet covers some essential tasks and concepts. Keep in mind that JavaFX is a versatile library, and there are many more features and components available. For detailed information and examples, refer to the official JavaFX documentation.