JavaFX_get_set_Properties1
  • Java Class
    • fields : state of objects , usually declared as private, to be moderated through public methods
    • methods: these are exposed to client with get/set , which corresponds to read/write respectively.
  • Java supports the fields of both primitive data-types  and special  serializable Java Bean (observable)
  • This examples uses primitive data-types
  • Binding data to JavaFX control:
    Label label1 = new Label(emp2.getfirstName());
Code :

//JavaFXTemplate1
package javafxtemplate1;
// without a divider
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.layout.TilePane;

public class JavaFXTemplate1 extends Application {
//

@Override

public void start(Stage primaryStage)
{
//
EMP2 emp2 = new EMP2();
emp2.setfirstName("John");
emp2.setlastName("Smith");
emp2.setSalary(2500.55);
//
Label label1 = new Label(emp2.getfirstName());
Label label2 = new Label(emp2.getlastName());
Label label3 = new Label(emp2.getSalary().toString());
TilePane tPane = new TilePane(); // scenelayout package TilePane
tPane.setVgap(8); tPane.setHgap(10);
tPane.getChildren().addAll(label1, label2, label3);
//
Scene scene = new Scene(tPane, 300, 250, Color.WHITE);
primaryStage.setTitle("Properties :: Binding");
primaryStage.setScene(scene);
primaryStage.show();
}

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

}

}

//class EMP2

package javafxtemplate1;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

/**
*
* @author Manas14
*/
public class EMP2 {
private String firstName; private String lastName;
private Double Salary;
public EMP2(){ // no parameters
}
/*
private EMP2(String fName, String lName, Double pSal) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.Salary = new SimpleDoubleProperty(pSal);
}
*/
public String getfirstName() {
return firstName;
}
public void setfirstName(String fName) {
this.firstName =fName;
}

public String getlastName() {
return lastName;
}
public void setlastName(String lName) {
this.lastName =lName;
}

public Double getSalary() {
return Salary;
}
public void setSalary(Double pSal) {
this.Salary = pSal;
}
}
//