Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

Example: Write a simple JavaFX program to bind textfield value with a label to be displayed.

LISTING 14.7 NodeStyleRotateDemo.java


public void start(Stage primaryStage) {
// Create a scene and place a button in the scene
StackPane pane = new StackPane();
Button btOK = new Button("OK");
btOK.setStyle("-fx-border-color: blue;");
pane.getChildren().add(btOK);

pane.setRotate(45);
pane.setStyle( "-fx-border-color: red;
-fx-background-color: lightgray;");

Scene scene = new Scene(pane, 200, 250);


The Color Class
The Color class can be used to create colors.
JavaFX defines the abstract Paint class for painting a node. The javafx.scene.paint.Color

The Font Class


A Font describes font name, weight, and size.
You can set fonts for rendering the text. The javafx.scene.text.Font class
Font font1 = new Font("SansSerif", 16);
Font font2 = Font.font("Times New Roman", FontWeight.BOLD,
FontPosture.ITALIC, 12);

The Image and Image-View class

 The javafx.scene.image.Image class represents a graphical image and is used for loading an
image from a specified filename or a URL.
 The javafx.scene.image.ImageView is a node for displaying an image. An ImageView can
be created from an Image object.

Alternatively, you can create an ImageView directly from a file or a URL as follows:

ImageView imageView = new ImageView("image/us.gif");

OR
new Image("http://www.cs.armstrong.edu/liang/image/us.gif");

Pane pane = new HBox(10);


pane.setPadding(new Insets(5, 5, 5, 5));
Image image = new Image("image/india.png");
pane.getChildren().add(new ImageView(image));

ImageView imageView2 = new ImageView(image);


imageView2.setFitHeight(100);
imageView2.setFitWidth(100);
pane.getChildren().add(imageView2);

ImageView imageView3 = new ImageView(image);


imageView3.setRotate(90);
pane.getChildren().add(imageView3);
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;

FlowPane

GridPane
There are various events in JavaFX i.e. MouseEvent, KeyEvent, ScrollEvent, DragEvent, etc.
We can also define our own event by inheriting the class javafx.event.Event.

The interface javafx.event.EventHandler must be implemented by all the event handlers and
filters.

Registering Handlers and Handling Events – Inner Class, Anonymous Inner


Class, Lambda Expressions
LAMBDA EXPRESSIONS – MOUSE EVENT ,KEY EVENT

KEY EVENT

package Chapter13;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class ShowFlowPane extends Application {


@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a pane and set its properties
FlowPane pane = new FlowPane();
pane.setPadding(new Insets(11, 12, 13, 14));
pane.setHgap(5);
pane.setVgap(5);
pane.setAlignment(Pos.CENTER);
pane.setOrientation(Orientation.VERTICAL);

// Place nodes in the pane


pane.getChildren().addAll(new Label("First Name:"), new TextField(), new
Label("MI:"));
TextField tfMi = new TextField();
tfMi.setPrefColumnCount(1);
pane.getChildren().addAll(tfMi, new Label("Last Name:"), new
TextField());

// Create a scene and place it in the stage


Scene scene = new Scene(pane, 200, 250);
primaryStage.setTitle("ShowFlowPane"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
launch(args);
}
}

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this
template
*/
package javafxapplication4;
import static javafx.application.Application.launch;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.scene.layout.GridPane;

public class ShowGridPane2 extends Application {


public void start(Stage primaryStage) {
// Create a pane and set its properties
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
pane.setHgap(5.5);
pane.setVgap(5.5);
// Place nodes in the pane
pane.add(new Label("00"), 0, 0);
pane.add(new Label("01"), 1, 0);
pane.add(new Label("10"), 0, 1);
pane.add(new Label("11"), 1, 1);
pane.add(new Label("20"), 0, 2);
pane.add(new Label("21"), 1, 2);
Label lblAdd = new Label("GRID PANE");
pane.add(lblAdd, 0, 3);
GridPane.setHalignment(lblAdd, HPos.RIGHT);
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("ShowGridPane"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
launch(args);
}
}

package javafxapplication4;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class ShowHboxVbox extends Application {


@Override
public void start(Stage primaryStage) {
// Create a border pane
BorderPane pane = new BorderPane();
// Place nodes in the pane
pane.setTop(getHBox());
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("ShowHBoxVBox");
primaryStage.setScene(scene);
primaryStage.show();
}
private HBox getHBox() {
HBox hBox = new HBox(15);
hBox.setPadding(new Insets(15, 15, 15, 15));
hBox.getChildren().add(getVBox());
ImageView imageView = new ImageView(new Image("E:\\Netbeans
Projects\\JavaFXApplication4\\src\\javafxapplication4\\flag.png"));
hBox.getChildren().add(imageView);
return hBox;
}
private VBox getVBox() {
VBox vBox = new VBox(15);
vBox.setPadding(new Insets(15, 5, 5, 5));
vBox.getChildren().add(new Label("Country:"));
vBox.getChildren().add(new Label("India"));
return vBox;
}
public static void main(String[] args) {
launch(args); }
}

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Line;

public class ShowLine extends Application {


@Override
public void start(Stage primaryStage) {
StackPane pane = new StackPane();
Line line1 = new Line();
line1.setStartX(10);
line1.setStartY(10);
line1.setEndX(160);
line1.setEndY(160);
line1.setStrokeWidth(5);
line1.setStroke(Color.PURPLE);
line1.setOpacity(0.7);
line1.setRotate(90);

Line line2 = new Line(10,10,160,160);


pane.getChildren().add(line1);
pane.getChildren().add(line2);

// Create a scene and place it in the stage


Scene scene = new Scene(pane);
primaryStage.setTitle("ShowLine");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args); }
}

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Text;
import javafx.scene.shape.Rectangle;

public class ShowRectangle extends Application {


@Override
public void start(Stage primaryStage) {
// Create rectangles
Rectangle r1 = new Rectangle(25, 10, 60, 30);
Rectangle r2 = new Rectangle(25, 50, 60, 30);
r2.setArcWidth(15);
r2.setArcHeight(25);

// Create a group and add nodes to the group


Group group = new Group();
group.getChildren().addAll(
new Text(10, 27, "r1"), r1, new Text(10, 67, "r2"), r2);

for (int i = 0; i < 4; i++) {


Rectangle r = new Rectangle(100, 50, 100, 30);
r.setRotate(i * 360 / 8);
r.setStroke(Color.color(Math.random(), Math.random(),Math.random()));
r.setFill(Color.WHITE);
group.getChildren().add(r);
}

// Create a scene and place it in the stage


Scene scene = new Scene(new BorderPane(group), 250, 150);
primaryStage.setTitle("ShowRectangle");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.text.Text;

public class ShowArc extends Application {


@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
Arc arc1 = new Arc(150, 100, 80, 80, 30, 35); // Create an arc
arc1.setFill(Color.YELLOW); // Set fill color
arc1.setType(ArcType.ROUND); // Set arc type
Arc arc2 = new Arc(150, 100, 80, 80, 30 + 90, 35);
arc2.setFill(Color.WHITE);
arc2.setType(ArcType.OPEN);
arc2.setStroke(Color.BLACK);

Arc arc3 = new Arc(150, 100, 80, 80, 30 + 90, 35);


arc3.setType(ArcType.CHORD);
arc3.setFill(Color.PINK);

// Create a group and add nodes to the group


VBox group = new VBox();
group.getChildren().addAll(new Text("arc1: round"), arc1, new Text("arc2:
open"), arc2,new Text("arc3: chord"), arc3);

// Create a scene and place it in the stage


Scene scene = new Scene(new BorderPane(group), 300, 200);
primaryStage.setTitle("ShowArc"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}

public static void main(String[] args) {


launch(args);
}
}

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.shape.Polygon;

public class ShowPolygon extends Application {

/**
*
* @param stage
*/
@Override
public void start(Stage stage) {
//Creating a Polygon

Polygon hexagon = new Polygon();


Polygon triangle = new Polygon();
//Adding coordinates to the hexagon
hexagon.getPoints().addAll(new Double[]{
200.0, 50.0,
400.0, 50.0,
450.0, 150.0,
400.0, 250.0,
200.0, 250.0,
150.0, 150.0,
});

triangle.getPoints().setAll(
200.0, 200.0,
300.0, 300.0,
200.0, 300.0 );

GridPane root = new GridPane();


root.add(triangle,0,0);
root.add(hexagon,1,0);

root.setStyle("-fx-bordor-color:green");

// Group root = new Group();


// root.getChildren().add(triangle);
Scene scene = new Scene(root, 600, 300);
stage.setTitle("Drawing a Polygon");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

INNER CLASS
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class ControlCircle extends Application {
private final Circle circle = new Circle(50);

@Override // Override the start method in the Application class


public void start(Stage primaryStage) {
// Hold two buttons in an HBox
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btEnlarge = new Button("Enlarge");
hBox.getChildren().add(btEnlarge);

// Create and register the handler


btEnlarge.setOnAction(new EnlargeHandler());
BorderPane borderPane = new BorderPane();
borderPane.setCenter(circle);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);

// Create a scene and place it in the stage


Scene scene = new Scene(borderPane, 200, 150);
primaryStage.setTitle("ControlCircle"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
class EnlargeHandler implements EventHandler<ActionEvent> {
@Override // Override the handle method
public void handle(ActionEvent e) {

circle.setRadius(circle.getRadius() + 2);
}
}
public static void main(String[] args) {
launch(args);
}
}

ANONYMOUS INNER CLASS


/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this
template
*/
package javafxapplication4;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;

public class Anonymous2 extends Application{


@Override // Override the start method in the Application class
public void start(javafx.stage.Stage primaryStage) {
// Hold two buttons in an HBox
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btNew = new Button("New");
Button btOpen = new Button("Open");
hBox.getChildren().addAll(btNew, btOpen);

// Create and register the handler


btNew.setOnAction(new EventHandler<ActionEvent>() {
@Override // Override the handle method
public void handle(ActionEvent e) {
System.out.println("Process New");
}
});

btOpen.setOnAction(new EventHandler<ActionEvent>() {
@Override // Override the handle method
public void handle(ActionEvent e) {
System.out.println("Process Open");
}
});

// Create a scene and place it in the stage


Scene scene = new Scene(hBox, 300, 50);
primaryStage.setTitle("AnonymousHandlerDemo"); // Set title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
launch(args);
}
}
Lambda- mouse key events

package Chapter13;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class MouseEventDemo extends Application {


@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a pane and set its properties
Pane pane = new Pane();
Text text = new Text(20, 20, "Programming is fun");
pane.getChildren().addAll(text);
text.setOnMouseDragged(e -> {
text.setX(e.getX());
text.setY(e.getY());
});

// Create a scene and place it in the stage


Scene scene = new Scene(pane, 300, 100);
primaryStage.setTitle("MouseEventDemo"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}

/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}

package Chapter13;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class KeyEventDemo extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a pane and set its properties
StackPane pane = new StackPane();
Text text = new Text(20, 20, "A");
pane.getChildren().add(text);
text.setOnKeyPressed(e -> {
text.setText(e.getText());
});
// Create a scene and place the pane in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("KeyEventDemo"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
text.requestFocus(); // text is focused to receive key input
}

/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}

package Chapter13;
import javafx.animation.FadeTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FadeTransitionAnimation extends Application {


@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Place an ellipse to the pane
StackPane pane = new StackPane();
Circle c = new Circle(50);
pane.getChildren().add(c);
// Apply a fade transition to ellipse
FadeTransition ft =
new FadeTransition(Duration.millis(3000), c);
ft.setFromValue(1.0);
ft.setToValue(0.1);
ft.setCycleCount(Timeline.INDEFINITE);
ft.setAutoReverse(true);
ft.play(); // Start animation

// Control animation
c.setOnMousePressed(e -> ft.pause());
c.setOnMouseReleased(e -> ft.play());

// Create a scene and place it in the stage


Scene scene = new Scene(pane, 200, 150);
primaryStage.setTitle("FadeTransitionDemo"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage

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

package Chapter13;
import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;
public class PathTransitionAnimation extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a pane
Pane pane = new Pane();

// Add an image view and add it to pane


ImageView imageView = new ImageView("pixabay8.jpg");
pane.getChildren().add(imageView);

// Create a path transition


PathTransition pt = new PathTransition(Duration.millis(10000),
new Line(100, 200, 100, 100), imageView);
pt.setCycleCount(5);
pt.play(); // Start animation

// Create a scene and place it in the stage


Scene scene = new Scene(pane, 250, 200);
primaryStage.setTitle("ImageRisingAnimation"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
launch(args);
}
}

You might also like