You are on page 1of 22

OBJECT-ORIENTED PROGRAMMING-2

1 JAVA
GRAPHICAL USER INTERFACES
2
JAVA FX VS SWING
Java Swing JavaFX

Swing is the standard toolkit for Java developers in creating GUI JavaFX grants platform assistance for designing desktop applications.

JavaFX has a fair amount of UI components available but is more inferior to


Swing has a further refined collection of GUI components
what Swing provides.

Swing is a legacy library that fully features and provides pluggable UI JavaFX has UI components that are still growing with a further advanced look
components and feel.

Swing can implement UI components with a conventional look and feel. JavaFX can provide rich internet applications having a modern UI.

JavaFX has also support from numerous IDEs as well, but it is not as
Swing has various IDEs, which offer a tool for rapid development.
experienced as Swing.

A swing was renamed from Java Foundation Classes, and sun microsystems JavaFX was initially released in December 2008 by Sun microsystem and is
announced it in the year 1997 now acquired by Oracle.

Events in JavaFX are higher thought-out and additional consistent than their
Events in Swing aren’t that consistent
equivalents in Swing.

JavaFX has fundamental maintenance for signature gestures resembling


Swing lacks any support for modern touch devices.
scrolling, swiping, rotating, and zooming.

Swing has basic controls like buttons, checkboxes, and bandboxes. JavaFX has many eye-catching controls that Swing doesn’t have
GRAPHICAL USER INTERFACES
JAVA FX
○ Platform independent

○ FXML : HTML-like declarative markup language : specify a UI

○ Scene builder

○ Built-in UI controls,

○ CSS styling

○ Rich set of APIs

4
JAVA FX ARCHITECTURE COMPONENTS
1. JavaFX API
1. Topmost layer with classes for building rich JavaFX applications.
2. Key packages:
1. javafx.animation: Transition-based animations for nodes.

2. javafx.css: Adding CSS-like styling to GUI applications.

3. javafx.geometry: Handling 2D shapes and operations.

4. javafx.scene: Building the scene graph, including sub-packages.

5. javafx.application: Managing the application's lifecycle.

6. javafx.event: Handling and managing events.

7. javafx.stage: Top-level containers for the application.

2. Scene Graph
1. Foundation of GUI Applications in JavaFX.
2. Comprises nodes, the basic building blocks.
3. Nodes form a hierarchical tree describing the UI.
4. Nodes can have various attributes like Effects, Opacity, Transforms, and Event Handlers.
5. Three general types of nodes.
SIMPLE JAVA FX APPLICATION : A BUTTON

Part 1: Prepare the class

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

public class SimpleJavaFXExample extends Application {


SIMPLE JAVA FX APPLICATION : A BUTTON
Part 2: Button creation and event handling
@Override
public void start(Stage primaryStage) {
Button button = new Button("Click Me");

button.setOnAction(e -> {
button.setText("Hello, JavaFX!");
});

StackPane root = new StackPane();


root.getChildren().add(button);

Scene scene = new Scene(root, 300, 200);


primaryStage.setTitle("Simple JavaFX Example");
primaryStage.setScene(scene);
primaryStage.show();
}
SIMPLE JAVA FX APPLICATION : A BUTTON

Part 3: Run the application

public static void main(String[] args) {


launch(args);
}
}
JAVAFX TEXT INPUT AND BUTTON EXAMPLE
public class TextInputButtonExample extends Application {

@Override
public void start(Stage primaryStage) { Create a text input field and a button.
TextField textField = new TextField();
Button button = new Button("Show Text");

VBox root = new VBox(10); // Vertical layout


root.getChildren().addAll(textField, button);

Scene scene = new Scene(root, 300, 200);


primaryStage.setTitle("Text Input and Button Example");
primaryStage.setScene(scene);
primaryStage.show();
}
JAVAFX TEXT INPUT AND BUTTON EXAMPLE
button.setOnAction(e -> {
String enteredText = textField.getText();
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Entered Text");
Event Handling
alert.setHeaderText(null);
alert.setContentText("You entered: " + enteredText);
alert.showAndWait();
});
}

public static void main(String[] args) {


launch(args);
}
}
JAVAFX TEXT INPUT AND BUTTON EXAMPLE
multiplyButton.setOnAction(e -> {
try {
double inputValue = Double.parseDouble(inputField.getText());
double result = inputValue * 3;
resultLabel.setText("Result: " + result);
} catch (NumberFormatException ex) {
Manipulating the input Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText(null);
alert.setContentText("Please enter a valid double value.");
alert.showAndWait();
}
});
}

public static void main(String[] args) {


launch(args);
}
}
LAYOUTS IN JAVAFX
public void start(Stage primaryStage) { VBox Layout
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
Button button3 = new Button("Button 3");

VBox vbox = new VBox(10); // Vertical layout


vbox.getChildren().addAll(button1, button2, button3);

Scene scene = new Scene(vbox, 300, 200);


primaryStage.setTitle("VBox Layout Example");
primaryStage.setScene(scene);
primaryStage.show();
HBOX LAYOUT

Button button4 = new Button("Button 4");


Button button5 = new Button("Button 5");
Button button6 = new Button("Button 6");

HBox hbox = new HBox(10); // Horizontal layout


hbox.getChildren().addAll(button4, button5, button6);

Scene scene = new Scene(hbox, 300, 200);


primaryStage.setTitle("HBox Layout Example");
primaryStage.setScene(scene);
primaryStage.show();
}
BORDERPANE LAYOUT
Button topButton = new Button("Top");
Button leftButton = new Button("Left");
Button centerButton = new Button("Center");
Button rightButton = new Button("Right");
Button bottomButton = new Button("Bottom");

BorderPane borderPane = new BorderPane();


borderPane.setTop(topButton);
borderPane.setLeft(leftButton);
borderPane.setCenter(centerButton);
borderPane.setRight(rightButton);
borderPane.setBottom(bottomButton);

Scene scene = new Scene(borderPane, 300, 200);


primaryStage.setTitle("BorderPane Layout Example");
primaryStage.setScene(scene);
primaryStage.show();
GRIDPANE LAYOUT
Button button7 = new Button("Button 7");
Button button8 = new Button("Button 8");
Button button9 = new Button("Button 9");

GridPane gridPane = new GridPane();


gridPane.add(button7, 0, 0);
gridPane.add(button8, 1, 0);
gridPane.add(button9, 2, 0);

Scene scene = new Scene(gridPane, 300, 200);


primaryStage.setTitle("GridPane Layout Example");
primaryStage.setScene(scene);
primaryStage.show();
OPEN A NEW WINDOW FROM A BUTTON

public void start(Stage primaryStage) {


primaryStage.setTitle("Main Window");

Button openButton = new Button("Open New Window");


openButton.setOnAction(e -> openNewWindow());

StackPane root = new StackPane(); private void openNewWindow() {


Stage newStage = new Stage();
root.getChildren().add(openButton); newStage.setTitle("New Window");

Scene scene = new Scene(root, 300, 200); // Create content for the new window
primaryStage.setScene(scene); StackPane newLayout = new StackPane();
Scene newScene = new Scene(newLayout, 300, 200);
primaryStage.show(); newStage.setScene(newScene);
}
// Show the new window
newStage.show();
}
EVENT HANDLING: KEY EVENTS

import javafx.event.EventHandler;
import javafx.scene.input.KeyEvent;

// Inside the start method


scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
System.out.println("Key pressed: " + event.getCode());
}
});
EVENT HANDLING: KEY EVENTS
scene.setOnKeyPressed(event -> {
Translate translate = new Translate();
KeyCode keyCode = event.getCode(); label.getTransforms().add(translate);

switch (keyCode) {
case LEFT:
translate.setX(translate.getX() - stepSize);
break;
case RIGHT:
translate.setX(translate.getX() + stepSize);
break;
case UP:
translate.setY(translate.getY() - stepSize);
break;
case DOWN:
translate.setY(translate.getY() + stepSize);
break;
default:
break;
}
});
EVENT HANDLING: MOUSE MOTION

scene.setOnMouseMoved(event -> {
double x = event.getX();
double y = event.getY();
coordinatesLabel.setText("Mouse
Coordinates: (" + x + ", " + y + ")");
});
DRAW ON MOUSE DRAGGED

private void drawPoint(double x, double y)


{
gc.setFill(javafx.scene.paint.Color.BLACK);
gc.fillOval(x, y, 2, 2); // Adjust the size
of the point as needed
} canvas = new Canvas(400, 400);
gc = canvas.getGraphicsContext2D();

scene.setOnMouseDragged(event -> {
double x = event.getX();
double y = event.getY();
drawPoint(x, y); private void clearCanvas() {
}); gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
}
DRAW IN JAVAFX

private void drawLine(double x1, double y1, double x2, double y2)
{
gc.setStroke(javafx.scene.paint.Color.BLACK);
gc.setLineWidth(2); // Adjust the line width as needed
gc.strokeLine(x1, y1, x2, y2);
}
ADDING AN IMAGE IN JAVAFX

// Load the image


Image image = new Image("file:image.png"); // Assuming
image.png is in the same directory

// Create an ImageView
ImageView imageView = new ImageView(image);
imageView.setFitWidth(200);
imageView.setFitHeight(150);
imageView.setPreserveRatio(true);

You might also like