Mouse Events
Mouse Events
Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
rect.setArcWidth(20);
rect.setArcHeight(20);
rect.setFill(Color.BLUE);
Text text = new Text (20,20,"Click on the rectangle to change its color");
rect.setOnMouseClicked(e->{
rect.setFill(Color.RED);
});
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
}
}
Output
Layouts
• For using the layout we must import the package javafx.scene.layout. The class
named Pane is the base class for all the layouts in JavaFX.
For example - if we want the spacing between the created nodes then we use
root.setSpacing(10);
root.getChildren().addAll(<NodeObjects>);
FlowPane
• A JavaFX FlowPane is a layout component which lays out its child components
either vertically or horizontally, and which can wrap the components onto the next
row or column if there is not enough space in one row.
Programming Example
package myjavafxapplication;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
@Override
public void start(Stage primaryStage) {
root.getChildren().add(B1);
root.getChildren().add(B2);
root.getChildren().add(B3);
root.getChildren().add(B4);
root.getChildren().add(B5);
root.getChildren().add(B6);
primaryStage.setScene(scene);
primaryStage.setTitle("FlowPane Demo");
primaryStage.show();
launch(args);
}
HBox
• The HBox layout arranges the children in the form of horizontal rows.
Programming Example
package myjavafxapplication;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
@Override
Output
primaryStage.setTitle("HBOX Demo");
primaryStage.show();
launch(args);
(2) The layout pane is set as HBox. All the button elements are attached to the
HBox pane by using getChildren().addAll function.
(4) Using setScene method this scene is then added to the PrimaryStage.
VBox
• The VBox layout arranges the children in the form of vertical rows.
• The VBox class extends Pane class.
Programming Example
package myjavafxapplication;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@Override
public void start (Stage primaryStage) {
Output
primaryStage.setScene(scene);
primaryStage.setTitle("VBOX Demo");
primaryStage.show();
launch(args);
}
BorderPane
• BorderPane lays out children in top, left, right, bottom, and center positions.
(2) BorderPane (Node c): Creates a new Border Pane with specified node at center.
(3) BorderPane (Node center, Node top, Node right, Node bottom, Node left):
Creates an BorderPane layout with the given Nodes to use for each of the main
layout areas of the Border Pane.
Programing Example
package myjavafxapplication;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
@Override
Output
root.setTop(B_top);
root.setCenter(B_center);
root.setBottom (B_bottom);
root.setLeft(B_left);
root.setRight (B_right);
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
}
StackPane
• StackPane is a layout in which every new node is placed on the top of previous
node.
Programming Example
package myjavafxapplication;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
@Override
Output
root.getChildren().addAll(B1,B2,B3);
primaryStage.setScene(scene);
primaryStage.setTitle("StackPane Demo");
primaryStage.show();
launch(args);
GridPane
GridPane places its nodes into a grid of rows and columns. Nodes may span
multiple rows or columns. GridPane is the most flexible built-in layout pane.
Programming Example
package myjavafxapplication;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
@Override
grid.add(B1,0,0);
grid.add(B2,1,0);
grid.add(B3,2,0);
grid.add(B4,0,1);
grid.add(B5,1,1);
Output
grid.add(B6,2,1);
primaryStage.setScene(scene);
primaryStage.setTitle("GridPane Demo");
primaryStage.show();