[go: up one dir, main page]

0% found this document useful (0 votes)
75 views7 pages

JavaFX Session-3

This document provides a comprehensive guide for setting up JavaFX in IntelliJ IDEA, including installation steps for IntelliJ, Java SDK, and JavaFX SDK. It details the project setup process, including adding JavaFX libraries and configuring VM options, along with sample code for creating a simple student form application using JavaFX components. Additionally, it covers layout options like VBox, HBox, and GridPane, with mini tasks for practical application of the concepts learned.

Uploaded by

Sebin Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views7 pages

JavaFX Session-3

This document provides a comprehensive guide for setting up JavaFX in IntelliJ IDEA, including installation steps for IntelliJ, Java SDK, and JavaFX SDK. It details the project setup process, including adding JavaFX libraries and configuring VM options, along with sample code for creating a simple student form application using JavaFX components. Additionally, it covers layout options like VBox, HBox, and GridPane, with mini tasks for practical application of the concepts learned.

Uploaded by

Sebin Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

JavaFX Setup in IntelliJ IDEA (2025 Guide)

1. Install IntelliJ IDEA

• Use IntelliJ IDEA Community (free).

• Download from: https://www.jetbrains.com/idea/

2. Install Java SDK

• Install Java 17 or above (JavaFX is compatible up to JDK 21).

• https://adoptium.net/ → Choose Temurin JDK 17

3. Download JavaFX SDK

• Get it from: https://gluonhq.com/products/javafx/

• Choose the right OS version (Windows, macOS, Linux)

• Extract the folder and keep path handy (e.g., C:\javafx-sdk-21)

4. IntelliJ Project Setup for JavaFX

Step-by-step:

1. Create a new Project

o File → New → Project

o Choose Java, name it e.g. StudentFormApp

o Select correct JDK (e.g., 17)

2. Add JavaFX Libraries

o Go to File → Project Structure → Libraries → + → Java

o Select the lib folder inside the downloaded JavaFX SDK

o Apply and OK

3. Set VM Options

o Go to Run → Edit Configurations

o In VM options, add:

o --module-path /path/to/javafx-sdk/lib --add-modules


javafx.controls,javafx.fxml

(Replace /path/to/javafx-sdk/lib with actual path)


1. JavaFX Basics
• Show simple Application subclass

• Explain start(Stage stage), Scene, VBox, Button

Sample Code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class StudentFormApp extends Application {


@Override
public void start(Stage stage) {
// Form fields
TextField nameField = new TextField();
TextField ageField = new TextField();
Button submitBtn = new Button("Submit");

Label output = new Label();

submitBtn.setOnAction(e -> {
String name = nameField.getText();
String age = ageField.getText();
output.setText("Student: " + name + ", Age: " +
age);
});

// Layout
VBox layout = new VBox(10);
layout.getChildren().addAll(
new Label("Name:"), nameField,
new Label("Age:"), ageField,
submitBtn,
output
);

Scene scene = new Scene(layout, 300, 250);


stage.setTitle("Student Form");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Mini Task (Student Exercise)

Objective: Build a form with fields like:

• Name, Age, Department, Email

• Show data in a Label or Alert dialog on button click

Optional:

• Use GridPane for layout

• Add basic input validation (if field is empty)


JavaFX Notes: Labels, TextFields, Buttons, Events,
Layouts
Goal:

Understand how to create a simple form using JavaFX with Labels, TextFields, Buttons,
handle Events, and apply Layouts: VBox, HBox, GridPane.

1. JavaFX Concepts
Key Components:

• Label: Displays text.


• TextField: Allows user text input.
• Button: Triggers actions.
• Event Handling: Code that runs when a user interacts with a UI element.

Layouts:

• VBox: Vertically arranges elements.


• HBox: Horizontally arranges elements.
• GridPane: Arranges elements in a grid of rows and columns.

2. Project Setup in IntelliJ IDEA


Step-by-Step Guide:

1. Create a new Java project:


o File → New → Project → Java
o Name it StudentFormApp
2. Add JavaFX Library:
o Download JavaFX SDK from https://gluonhq.com/products/javafx/
o Go to File → Project Structure → Libraries → + → Java
o Select lib folder of JavaFX SDK
3. Configure VM Options:
o Run → Edit Configurations → Add VM options:
o --module-path /path/to/javafx-sdk/lib --add-modules
javafx.controls,javafx.fxml

Replace /path/to/... with your actual JavaFX SDK path.

3. Sample Code: Student Form Using VBox


import javafx.application.Application
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class StudentFormApp extends Application {


@Override
public void start(Stage stage) {
// Create form components
Label nameLabel = new Label("Name:");
TextField nameField = new TextField();

Label ageLabel = new Label("Age:");


TextField ageField = new TextField();

Button submitButton = new Button("Submit");


Label outputLabel = new Label();

// Event handling
submitButton.setOnAction(e -> {
String name = nameField.getText();
String age = ageField.getText();
outputLabel.setText("Student: " + name + ", Age:
" + age);
});

// Layout using VBox


VBox layout = new VBox(10);
layout.getChildren().addAll(
nameLabel, nameField,
ageLabel, ageField,
submitButton,
outputLabel
);

Scene scene = new Scene(layout, 300, 250);


stage.setTitle("Student Form - VBox Layout");
stage.setScene(scene);
stage.show();
}

public static void main(String[] args) {


launch(args);
}
}

Mini Task (VBox): Add a third field like Department, and include it in the output.
4. Alternative Layout Examples
4.1 HBox Example
HBox hbox = new HBox(10);
hbox.getChildren().addAll(new Label("First:"), new
TextField(), new Label("Last:"), new TextField());

• Useful for placing elements side by side.

💡 Mini Task (HBox): Create a login form layout with Username: and Password: labels
and fields next to each other.

4.2 GridPane Example


GridPane grid = new GridPane();
grid.setVgap(10);
grid.setHgap(10);

grid.add(new Label("Name:"), 0, 0);


grid.add(new TextField(), 1, 0);
grid.add(new Label("Age:"), 0, 1);
grid.add(new TextField(), 1, 1);
grid.add(new Button("Submit"), 1, 2);

• Ideal for structured form layouts with rows and columns.

Mini Task (GridPane): Recreate the Student Form using GridPane, and align all labels and
inputs cleanly in two columns.

Layout Comparison Table


Layout Description Best Use Case

VBox Vertically stacks components Simple forms, linear stacking

HBox Horizontally arranges components Name + input side-by-side

GridPane Grid-like row-column structure Structured forms, labeled inputs

5. Running the App

1. Ensure VM options are set.


2. Click the green Run button.
3. A window will appear with the form.
4. Enter name and age → click Submit → result shows below the button.

You might also like