2 Replies Latest reply on Jun 5, 2013 5:00 PM by James_D

    How can I link EntityClass properties to GUI compoments?

      package pub.lne.entidades;
      import java.io.Serializable;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.Table;
      @Table(name = "CONTACT")
      public class Contact implements Serializable {
          private static final long serialVersionUID = 1L;
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          private Long id;
          @Column(name = "NAME")
          private String name;
          @Column(name = "PHONE")
          private String phone;
          @Column(name = "EMAIL")
          private String email;
          public Long getId() {
              return id;
          public void setId(Long id) {
              this.id = id;
          public String getName() {
              return name;
          public void setName(String name) {
              this.name = name;
          public String getPhone() {
              return phone;
          public void setPhone(String phone) {
              this.phone = phone;
          public String getEmail() {
              return email;
          public void setEmail(String email) {
              this.email = email;
      <?xml version="1.0" encoding="UTF-8"?>
      <?import java.lang.*?>
      <?import java.util.*?>
      <?import javafx.scene.*?>
      <?import javafx.scene.control.*?>
      <?import javafx.scene.layout.*?>
      <AnchorPane id="AnchorPane" prefHeight="308.0" prefWidth="433.0" xmlns:fx="http://javafx.com/fxml" fx:controller="pub.pre.vistas.ContactViewController">
          <TextField fx:id="txtName" layoutX="121.0" layoutY="102.0" prefWidth="200.0" />
          <TextField fx:id="txtPhone" layoutX="121.0" layoutY="133.0" prefWidth="114.0" />
          <TextField fx:id="txtEmail" layoutX="121.0" layoutY="162.0" prefWidth="200.0" />
          <Label layoutX="69.0" layoutY="105.0" text="Name" />
          <Label layoutX="69.0" layoutY="136.0" text="Phone" />
          <Label layoutX="69.0" layoutY="168.0" text="Email" />
          <Button fx:id="btnSaveContact" layoutX="122.0" layoutY="218.0" mnemonicParsing="false" onAction="#btnSaveContactAction" text="Save Contact" />
      package pub.pre.vistas;
      import java.net.URL;
      import java.util.ResourceBundle;
      import javafx.event.ActionEvent;
      import javafx.fxml.FXML;
      import javafx.fxml.Initializable;
      import javafx.scene.control.Button;
      import javafx.scene.control.TextField;
      import javax.persistence.EntityManager;
      import javax.persistence.EntityManagerFactory;
      import javax.persistence.Persistence;
      import pub.lne.entidades.Contact;
      public class ContactViewController implements Initializable {
          private ResourceBundle resources;
          private URL location;
          private Button btnSaveContact;
          private TextField txtEmail;
          private TextField txtName;
          private TextField txtPhone;
          private Contact contact;
          void btnSaveContactAction(ActionEvent event) {
              //setting values
              EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersistenceUnit");
              EntityManager em = emf.createEntityManager();
          public void initialize(URL url, ResourceBundle rb) {
              contact = new Contact();
      The Question
      How can I link the Contact.java properties to the ContactViewController.java components (In this case the textfields). In order to automatically update the Contact properties or viceversa.

      I dont want do this:
      There is any way to do it automatically ?
      If yes, please correct my example in this post with the best way.

      Edited by: m_ilio on Jun 5, 2013 8:44 AM
        • 1. Re: How can I link EntityClass properties to GUI compoments?
          Have a look at all the JavaBean***Property classes.

          You have to wrap you entity class and for each usual getter/setter you will have to provide an additional property in the JavaFX style with the help of the builder class.


          Then you can do (bidirectional) binding.

          For bidirectional binding, also notice this from the docs:

          If the Java Bean property is bound (i.e. it supports PropertyChangeListeners), this JavaBeanStringProperty will be aware of changes in the Java Bean. Otherwise it can be notified about changes by calling fireValueChangedEvent().
          1 person found this helpful
          • 2. Re: How can I link EntityClass properties to GUI compoments?
            Have a look at this article: http://asipofjava.blogspot.be/2013/05/javafx-properties-in-jpa-entity-classes.html in which the author encapsulates data in JavaFX properties inside an Entity.

            This looks fine for simple JPA based applications, but be aware that the JavaFX property classes are not Serializable, so this may not work if you are retrieving your entities from a remote EJB, for example.
            1 person found this helpful