MovieBazar.Info

Latest News & Reviews

JavaFX : Java Graphical Interface Button , Text , Field & Level .

How to work with buttons, text fields and labels in JavaFX. Java Graphical User Interface – How to Create a JavaFX Project in JavaFX Writing is a detailed discussion.

We’ll make a small tax calculator. Which will take a valid input from the text field, then click on the button to calculate the tax and label the tax. We’ll add a textfield, a level, and a button to the seedbillers. I will give an ID of each item.

Id can also be set from text mood. fx: id = “your_id”

Widgets that have been added to the Cyan Builder, such as buttons, text views and labels, to which a reference should be made. This is how to create:

1
2
3
4
5
6
7
TextField numberFiled;
Button btnCalculate;
Label lblResult;

numberFiled = (TextField) root.lookup (“# number”);
btnCalculate = (Button) root.lookup (“# calculate”);
lblResult = (Label) root.lookup (“# result”);
We created the ID that we set the ID. I forgot a lot of time because I forgot to # before a project ID.

The click linker is added to the button. When the button is clicked, the code inside it will run.

1
2
3
4
5
6
7
8
btnCalculate.setOnAction (new EventHandler () {
@Override
public void handle {ActionEvent event} {

// do anything

}
});
Data from the textfield is read as:

1
numberFiled.getText () .toString ()
Text is set in a label:

1
lblResult.setText (“Awesome!”);
Then we can set the result labeled by calculating the tax amount. Full Java File:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class Main extends Application {

TextField numberFiled;
Button btnCalculate;
Label lblResult;
Double tax;

@Override
public void start (Stage primaryStage) throws exception {
Parent root = FXMLLoader.load (getClass (). GetResource (“sample.fxml”));
primaryStage.setTitle (“Hello World”);
primaryStage.setScene (new Scene (root, 300, 275));
primaryStage.show ();

numberFiled = (TextField) root.lookup (“# number”);
btnCalculate = (Button) root.lookup (“# calculate”);
lblResult = (Label) root.lookup (“# result”);

btnCalculate.setOnAction (new EventHandler () {
@Override
public void handle {ActionEvent event} {

Double amount = Double.parseDouble (numberFiled.getText () .toString ());
tax = (amount * 0.05); // 5% tax

lblResult.setText (tax.toString ());

}
});

}

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

If we run the code, we will get such an application

Though these things seem very easy, they can be used to create many complex software. Then they can be shared with everyone if they want the software.

Leave a Reply

Your email address will not be published. Required fields are marked *

MovieBazar.Info © 2018