Properties_JavaBean_Primitive1
  • JavaFX Beans
    • In JavaFX, you use one of the JavaFX Bean property classes, than using primitive field types in a class.
    • By convention, getters (read) and setters (write) for a property of a field in a class are declared final. You may not use final ( in simple example like this one)
    • Property Interface
      •void bind(ObservableValue<? extends T> observable) :: Unidirectional binding
      • void unbind()
      • void bindBidirectional(Property<T> other)
      • void unbindBidirectional(Property<T> other)
      • boolean isBound()
       
  • Emp1
    • Constructor:
      Parameters in Constructor (setting fields)
      Emp1 emp1 = new Emp1("John", "Smith", 2500.25);
    • Java Bean
      import javafx.beans.property.DoubleProperty;
      import javafx.beans.property.SimpleDoubleProperty;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.property.StringProperty;
    • Bean Properties: Bindings
      StringProperty prop1 = new SimpleStringProperty("");
      prop1.bindBidirectional(emp1.firstNameProperty());
    • Bean field's value:
      • String = prop1.get()
      • Double To String::
        prop3.asString().get()
         
  • EMP2
    •  (Setting/writing Fields)  No Beans:
      emp2.setfirstName("John");
      emp2.setlastName("Smith");
      emp2.setSalary(2500.55);
    • Constructor:
      EMP2 emp2 = new EMP2();
EMP2 : Setting (writable ) Properties , Fields are primitive data-types

Emp1 : Getter/Setter (Read/Write) using javafx.bean

 

 
Code :


//class JavaFXTemplate1
package javafxtemplate1;
//
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;

/**
*
* @author Manas9
*/
public class JavaFXTemplate1 extends Application {
//

@Override

public void start(Stage primaryStage)
{
//
// Group groot = new Group();
VBox groot = new VBox();
EMP2 emp2 = new EMP2();
Emp1 emp1 = new Emp1("John", "Smith", 2500.25);
emp2.setfirstName("John");
emp2.setlastName("Smith");
emp2.setSalary(2500.55);
//
StringProperty prop1 = new SimpleStringProperty("");
prop1.bindBidirectional(emp1.firstNameProperty());
StringProperty prop2 = new SimpleStringProperty("");
prop2.bindBidirectional(emp1.lastNameProperty());
DoubleProperty prop3 = new SimpleDoubleProperty();
prop3.bindBidirectional(emp1.salaryproperty());
//
Label label1 = new Label(emp2.getfirstName());
Label label2 = new Label(emp2.getlastName());
Label label3 = new Label(emp2.getSalary().toString());
//
Label label4 = new Label(prop1.get());
Label label5 = new Label(prop2.get());
Label label6 = new Label(prop3.asString().get());
//
TilePane tPane1 = new TilePane(); // scenelayout package FlowPane
TilePane tPane2 = new TilePane();
tPane1.setVgap(20); tPane1.setHgap(20);
tPane2.setVgap(20); tPane2.setHgap(20);
tPane2.getChildren().addAll(label4, label5, label6);
tPane1.getChildren().addAll(label1, label2, label3);
//

groot.getChildren().addAll(tPane1, tPane2);
Scene scene = new Scene(groot, 300, 250, Color.WHITE);
primaryStage.setTitle("Properties :: Binding");
primaryStage.setScene(scene);
primaryStage.show();
}

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

}

}

// class Emp1

package javafxtemplate1;

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

/**
*
* @author Manas14
*/
public class Emp1 {
//
private final StringProperty firstName;
private final StringProperty lastName ;
private final DoubleProperty Salary;
public Emp1(String firstName, String lastName, Double salary) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
this.Salary = new SimpleDoubleProperty(salary);
}
//
public double getSalary(){
return Salary.get();
}
public void setSalary(Double Salary){
this.Salary.set(Salary);
}
public DoubleProperty salaryproperty()
{
return Salary;
}
//
public String getFirstName() {
return firstName.get();
}
//
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}

public StringProperty firstNameProperty() {
return firstName;
}

public String getLastName() {
return lastName.get();
}

public void setLastName(String lastName) {
this.lastName.set(lastName);
}

public StringProperty lastNameProperty() {
return lastName;
}

@Override
public String toString() {
return (getFirstName() + " "
+ getLastName() + " " + getSalary());
}

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

Displays:

//