Java Lab
Java Lab
JavaFX:-
Why JavaFX
JavaFX contains several features that make it a preferred choice for developing rich client
applications:
1|Page
JavaFX provides out of the box support for applying effects and animations, which are
important for developing gaming applications.
Built-in UI controls – JavaFX comprises all the major built in UI controls that help in
developing well featured applications.
CSS styling- just like websites use CSS for styling, JavaFX is also provides the feature to
integrate the application with CSS styling, etc.
JavaFX Application structure
Basic building blocks of JavaFX: stage, scene, and scene graph/nodes.
Stage:
Its top level container for all the JavaFX objects (scene). A stage I javafx represents the
primary window of a GUI application. The javafx stage is a desktop screen which displays a
javafx output to the user, also its said that stage is window.
Its represented by stage class of the package javafx.stage. By default the primary stage is
created by the platform itself.
The created stage object is passed as an argument to the start().
2|Page
- stage.show();
JavaFX scene:
Create scene
3|Page
JavaFX Node:
4|Page
5|Page
6|Page
7|Page
8|Page
JavaFX UI controls
Types of UI controls: Label, Button, Text Field, Text Area, password field, Radio button,
checkbox, combo Box, Menu button, Image View, File chooser, Input Dialogs, Alert Dialogs
9|Page
Label l2 = new Label("Password");
PasswordField pf1 = new PasswordField();
ta1.setPrefColumnCount(10);
ta1.setPrefRowCount(10);
gp.add(l2, 0, 1);
gp.add(pf1, 1, 1);
gp.add(l3, 0, 2);
gp.add(ta1, 1, 2);
gp.add(tb1, 0, 4);
gp.add(tb2, 1, 4);
gp.add(tb3, 2, 4);
10 | P a g e
Scene scene = new Scene(gp, 400, 350);
Stage st1 = new Stage();
st1.setTitle("Hello World!");
st1.setScene(scene);
st1.show();
}
public static void main(String[] args) {
launch(args);
}
}
Output
2. ChoiceBox
//Example Choicebox
package choicebox;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
11 | P a g e
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
@Override
public void start(Stage primaryStage) {
12 | P a g e
3. ComboBox
Example
package combobox;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Combobox extends Application {
@Override
public void start(Stage primaryStage) {
GridPane gp = new GridPane();
13 | P a g e
launch(args);
}
}
4. ListView
Example
package listview;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
@Override
public void start(Stage primaryStage) {
GridPane gp = new GridPane();
14 | P a g e
Label lb = new Label("Departmet");
//Label lb2 = new Label("Birth date");
//DatePicker dp = new DatePicker();
gp.add(lb, 0, 0);
gp.add(lv, 1, 0);
//gp.add(lb2, 0, 2);
//gp.add(dp, 1, 3);
Scene scene = new Scene(gp, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
5. DatePicker
Example
package listview;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
15 | P a g e
public class Listview extends Application {
@Override
public void start(Stage primaryStage) {
GridPane gp = new GridPane();
gp.add(lb2, 0, 2);
gp.add(dp, 1, 3);
16 | P a g e
Handling user events
//Example
package userevent;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Userevent extends Application {
@Override
public void start(Stage primaryStage) {
TextField txtf1 = new TextField();
Button bt1 = new Button("click here");
Label lb1 = new Label();
Label lb2 = new Label();
bt1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
lb1.setText("you have clicked button bt1'");
lb2.setText("Hello Dr. "+txtf1.getText());
17 | P a g e
}
});
18 | P a g e
Adding two numbers
//Example
package add2no;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Add2No extends Application {
@Override
public void start(Stage primaryStage) {
gp.add(lblentern2, 0, 1);
gp.add(txtn2, 1, 1);
gp.add(lblsum, 0, 2);
gp.add(lblDisplaysum, 1, 2);
gp.add(btnsum, 2, 2);
gp.setAlignment(Pos.CENTER);
Scene scene = new Scene(gp, 400, 350);
primaryStage.setTitle("AddTwoNumber!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
20 | P a g e
}
Output
package lab1calculator;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class Lab1calculator extends Application {
@Override
public void start(Stage primaryStage) {
//controls
Label lblnum1 = new Label("num1");
Label lblnum2 = new Label("num2");
Label lblresult = new Label("result");
21 | P a g e
TextField txtnum1 = new TextField();
TextField txtnum2 = new TextField();
TextField txtresult = new TextField();
//containers Or layout
HBox hbox1 = new HBox();
hbox1.setSpacing(10);
hbox1.setAlignment(Pos.CENTER);
HBox hbox2 = new HBox();
hbox2.setSpacing(20);
hbox2.setAlignment(Pos.CENTER);
VBox vbox = new VBox();
vbox.setSpacing(20);
//Adding controls to containers
hbox1.getChildren().addAll(lblnum1,txtnum1,lblnum2,txtnum2,lblresult,txtresult);
hbox2.getChildren().addAll(btnadd,btnsubtract,btnmultiply,btndivide,btnclear);
vbox.getChildren().addAll(hbox1,hbox2);
22 | P a g e
primaryStage.setScene(scene);
primaryStage.show();
//Handling user Action
btnadd.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
double n1,n2,res;
n1=Double.parseDouble(txtnum1.getText());
n2=Double.parseDouble(txtnum2.getText());
res=n1+n2;
txtresult.setText(res+"");
}
});
btnsubtract.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
double n1,n2,res;
n1=Double.parseDouble(txtnum1.getText());
n2=Double.parseDouble(txtnum2.getText());
res=n1-n2;
txtresult.setText(res+"");
}
});
btnmultiply.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
23 | P a g e
double n1,n2,res;
n1=Double.parseDouble(txtnum1.getText());
n2=Double.parseDouble(txtnum2.getText());
res=n1*n2;
txtresult.setText(res+"");
}
});
btndivide.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
double n1,n2,res;
n1=Double.parseDouble(txtnum1.getText());
n2=Double.parseDouble(txtnum2.getText());
res=n1/n2;
txtresult.setText(n2 == 0 ? "Undefined" : String.valueOf(res));
}
});
btnclear.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
txtnum1.setText("");
txtnum2.setText("");
txtresult.setText("");
}
});
24 | P a g e
}
public static void main(String[] args) {
launch(args);
}
}
Output
25 | P a g e