0% found this document useful (0 votes)
23 views

Java Lab

The document discusses JavaFX and its basic building blocks like stage, scene and nodes. It provides examples to create labels, text fields, buttons and handling user events in JavaFX. It also includes an example to add two numbers by getting input from text fields and displaying the output.

Uploaded by

dejenehundaol91
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Java Lab

The document discusses JavaFX and its basic building blocks like stage, scene and nodes. It provides examples to create labels, text fields, buttons and handling user events in JavaFX. It also includes an example to add two numbers by getting input from text fields and displaying the output.

Uploaded by

dejenehundaol91
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Java programing practical examples

GUI using Java FX in netbeans

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

1. Label, Text Field, Password Field, Text area, Toggle Button


//Examples on Label, Text Field, Password Field, Text area, Toggle Button
Steps for implementation
 To create a layout, you need to –
Create ode.
Instantiate the respective class of the required layout.
Set the properties of the layout.
Add all the created nodes to the layout.
Add layout to the scene
package uio;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Uio extends Application {
@Override
public void start(Stage primaryStage) {
Label l1 = new Label("Name");
TextField tf1 = new TextField();

9|Page
Label l2 = new Label("Password");
PasswordField pf1 = new PasswordField();

Label l3 = new Label("Biablography");


TextArea ta1 = new TextArea();

ta1.setPrefColumnCount(10);
ta1.setPrefRowCount(10);

ToggleButton tb1 = new ToggleButton("bottom");


ToggleButton tb2 = new ToggleButton("left");
ToggleButton tb3 = new ToggleButton("right");

GridPane gp = new GridPane();


gp.add(l1, 0, 0);
gp.add(tf1, 1, 0);

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;

public class Choiceox extends Application {

@Override
public void start(Stage primaryStage) {

GridPane gp = new GridPane();


ChoiceBox cb = new ChoiceBox();
cb.getItems().addAll("cs","IT","SW","civil");
Label lb = new Label("Departmet");
gp.add(lb, 0, 0);
gp.add(cb, 1, 0);
Scene scene = new Scene(gp, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

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();

ComboBox cb = new ComboBox();


cb.setEditable(true);
cb.getItems().addAll("cs","IT","SW","civil");
Label lb = new Label("Departmet");
gp.add(lb, 0, 0);
gp.add(cb, 1, 0);

Scene scene = new Scene(gp, 300, 250);


primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {

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;

public class Listview extends Application {

@Override
public void start(Stage primaryStage) {
GridPane gp = new GridPane();

ListView lv = new ListView();


lv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
lv.setPrefSize(100,120);
lv.getItems().addAll("cs","IT","SW","civil");

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();

ListView lv = new ListView();


lv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
lv.setPrefSize(100,120);
lv.getItems().addAll("cs","IT","SW","civil");
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);
}
}

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
}
});

GridPane gp = new GridPane();


gp.add(lb1, 0, 1);
gp.add(bt1, 0, 0);
gp.add(lb2, 0, 3);
gp.add(txtf1, 0, 2);
gp.setAlignment(Pos.CENTER);
Scene scene = new Scene(gp, 300, 250);

primaryStage.setTitle("User action performed!");


primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

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) {

Label lblentern1 = new Label("enter n1: ");


TextField txtn1 = new TextField();
Label lblentern2 = new Label("enter n2: ");
TextField txtn2 = new TextField();
Button btnsum = new Button("+");
Label lblsum = new Label("sum: ");
Label lblDisplaysum = new Label();
btnsum.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if((txtn1.getText().length()==0) && (txtn2.getText().length()==0))
lblDisplaysum.setText("" + 0.0);
else if(txtn1.getText().length()==0)
lblDisplaysum.setText("" + (0.0 + Double.parseDouble(txtn2.getText())));
19 | P a g e
else if(txtn2.getText().length()==0)
lblDisplaysum.setText("" + (0.0 + Double.parseDouble(txtn1.getText())));
else
lblDisplaysum.setText("" + (Double.parseDouble(txtn1.getText()) +
Double.parseDouble(txtn2.getText())));
}
});
GridPane gp = new GridPane();
gp.add(lblentern1, 0, 0);
gp.add(txtn1, 1, 0);

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

Simple calculator using Javafx

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();

Button btnadd = new Button("+");


Button btnsubtract = new Button("-");
Button btnmultiply = new Button("*");
Button btndivide = new Button("/");
Button btnclear = new Button("C");

//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);

Scene scene = new Scene(vbox,600,120);


primaryStage.setTitle("calculator!");

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

You might also like