Professional Documents
Culture Documents
JavaFX
A media/graphics framework for creating GUIs in Java applications.
Enables developers to design , create, test, debug, and deploy rich
client applications that operate consistently across diverse platforms.
More powerful than Swing
Used to create both desktop and web applications
Make use of FXML- new XML based mark-up
mark language for defining UIs
Steps
Import javafx.application.Application
Inherit Application in your class
Override the start method in the Application
• here u can start scene, layout or widgets
to start our java application , call this inside main method
• launch(args); );//calls the application constructor which interns starts the
application
Show the stage
JavaFX Terms
Control- set of objects.
Eg.Buttons,labels , Text
objects –Controls are
added to the layouts
Layouts-how the controls
are arranged
Scene- Layouts are added
to the scene
Stage- Scene is added to
the stage. Stage can have
several scenes
Event-triggered on some
action. Event handlers does
this.Eg:An event behind
every button
@Override
Example 1 public void start(Stage primaryStage) throws Ex
Button btn=new Button("Click me");
StackPane sp=new StackPane();
sp.getChildren().add(btn);
import javafx.application.Application;
Scene scene=new Scene(sp);
import javafx.scene.control.Button; primaryStage.setScene(scene);
import javafx.scene.layout.StackPane; primaryStage.setTitle("My First JavaFX App");
import javafx.stage.Stage; primaryStage.setWidth(500);
primaryStage.setHeight(300);
import javafx.scene.Scene;
primaryStage.show();
}
public class JavaFXApplication3 extends Application { }
de
d start(Stage primaryStage) throws Exception {
btn=new Button("Click me");
exit=new Button("Exit");
OnAction(new EventHandler<ActionEvent>(){
ic void handle(ActionEvent event){
System.out.println("HelloWorld");
out-defines the way in which the components
e seen on the stage
Description
rPane Organizes nodes in top, left, right, centre and the bottom of
screen.
ane Organizes the nodes in the horizontal rows according to the
available horizontal spaces. Wraps the nodes to the next line
the horizontal space is less than the total width of the nodes
ne Organizes the nodes in the form of rows and columns.
Organizes the nodes in a single row.
It is the base class for all the layout classes.
Pane Organizes nodes in the form of a stack i.e. one onto another
Organizes nodes in a vertical column.
Layout
rPane
erPane arranges the nodes at the left, right, centre, top and bottom of the screen.
ented by javafx.scene.layout.BorderPane class.
class provides various methods like setRight(),
setRight setLeft(), setCenter(), setBottom
p()
() which are used to set the position for the specified nodes.
ntiate BorderPane class to create the BorderPane layout.
Radiobutton getText()
returns the textLabel for
button
if (rb != null) {
String s = rb.getText();
if(opt1.isSelected()){
l.setText(opt1.getText()+"Selected");
(opt1.getText()+"Selected");
l.setFont(new
(new Font("Arial",20));
}
else
l.setText("Choose
("Choose an option");
}
});
Scene sc=new
=new Scene(root,200,200);
primaryStage.setScene
primaryStage.setScene(sc);
primaryStage.show
primaryStage.show();
}
CheckBox
Label l=new Label();
CheckBox opt1=new CheckBox("Tagore");
("Tagore");
CheckBox opt2=new CheckBox("Raman");
("Raman");
CheckBox opt3=new CheckBox("Khorana");
("Khorana");
CheckBox opt4=new CheckBox("Teresa");
("Teresa");
CheckBox opt5=new CheckBox("Chandrasekhar");
("Chandrasekhar");
CheckBox opt6=new CheckBox("Abhijit");
("Abhijit");
VBox root=new VBox();
Button btn=new
=new Button("submit");
root.getChildren().addAll(l,btn,opt1,opt2,opt3,opt4,opt5,opt6);
(l,btn,opt1,opt2,opt3,opt4,opt5,opt6);
nAction(new
ndler<ActionEvent>(){
if(opt5.isSelected()){
blic void handle(ActionEvent event){
selecteditems+=opt6.getText();
String selecteditems="";
selecteditems+=" "; }
f(opt1.isSelected()){
selecteditems+=opt1.getText();
if(
if(selecteditems.isEmpty())
l.setText("Select your option");
selecteditems+=" ";
else
f(opt2.isSelected()){
l.setText("You have selected"+ selectedi
selecteditems+=opt2.getText(); }
selecteditems+=" "; });
Scene sc=new
=new Scene(root,200,200);
f(opt3.isSelected()){ primaryStage.setScene
primaryStage.setScene(sc);
selecteditems+=opt3.getText(); primaryStage.show
primaryStage.show();
selecteditems+=" ";
f(opt4.isSelected()){
selecteditems+=opt4.getText();
selecteditems+=" ";
ComboBox
ComboBox<String> desert=new ComboBox<String>();
ComboBox
desert.getItems().add("Cake");
desert.getItems().add("Cookies");
().add("Cookies");
desert.getItems().add("Doughnuts");
().add("Doughnuts");
desert.getItems().add("Pies");
ComboBox
etOnAction(new EventHandler<ActionEvent
ActionEvent>()
primaryStage.setScene(scene1);
primaryStage.show();
}
}
Switching between Scenes-Example
Example-2
// Defining the Submit button
s Scenedemo extends Button submit = new Button("Su
n{ primaryStage throws Exception { submit.setOnAction(e -> {
public void start(Stage primaryStage)
dow; window = primaryStage; first = firstName.getText();
tton; window.setTitle("Flight
("Flight Assistant"); last = lastName.getText();
//scene 1 createNewScene();
arButton; // Creating a GridPane container });
ne, scene1; GridPane grid = new GridPane(); GridPane.setConstraints(submit
grid.setPadding(new
(new Insets(50, 50, 50, 50)); grid.getChildren().add(submit);
, last;
grid.setVgap(5);
grid.setHgap(5); // Defining the Clear button
ic void main(String[] args) clearButton = new Button("Clea
// Defining the Name text field GridPane.setConstraints(clearBu
final TextField firstName = new TextField();
TextField clearButton.setOnAction(event
args);
firstName.setPromptText("Enter
("Enter your first name."); firstName.clear();
GridPane.setConstraints(firstName
firstName, 0, 0); lastName.clear();
grid.getChildren().add(firstName); ); });
grid.getChildren().add(clearButt
// Defining the Last Name text field scene = new Scene(grid, 300, 15
final TextField lastName = new TextField();
TextField primaryStage.setScene(scene);
lastName.setPromptText("Enter
("Enter your last name."); primaryStage.show();
GridPane.setConstraints(lastName
lastName, 0, 1);
grid.getChildren().add(lastName); ); }
lic void createNewScene() {
abel title = new Label();
tle.setText("Welcome
("Welcome " + first + " " + last + " to Java Programming!");
tle.setFont(Font.font("Times New Roman", 38));
chorPane.setLeftAnchor(table1, 0.0);
chorPane.setRightAnchor(table2, 0.0);
ttomRow.getChildren().addAll(table1, table2);
ot.getChildren().addAll(menuBar, textArea, bottomRow);
bottomRow
ene scene = new Scene(root, 800, 800);
maryStage.setScene(scene);
maryStage.show();
ateTimePicker // show week numbers
class Datetime extends Application { d.setShowWeekNumbers(true);
static void main(String[] args) { // when datePicker is pressed
d.setOnAction(event);
unch(args);
JavaFX enables you to style JavaFX components using CSS, just like
you can style HTML element in web pages with CSS.
Scene Specific CSS Stylesheet
scene.getStylesheets().add("style1/button
().add("style1/button-styles.css");
//tells
tells JavaFX to look for a stylesheet file called button-styles.css
button
which is located in a directory called style1 .
JavaFX CSS Styling
This example sets the background color CSS property in the style
property to a blue color.
JavaFX CSS Styling
Button button3 = new Button("Button 3");
button3 .setStyle("-fx-background
background-color: pink; -fx-text-fill: white;");
Example
Button btn1 = new Button();
btn1.setText("Red");
btn1.setOnAction(new EventHandler<ActionEvent>()
EventHandler {
@Override
public void handle(ActionEvent
ActionEvent event) {
System.out.println("Red Color");
Color
btn1.setStyle("-fx-background
background-color: red; -fx-text-fill: white;");
}
});