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

    How can I link EntityClass properties to GUI compoments?

    m_ilio
      Contact.java
      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;
      
      @Entity
      @Table(name = "CONTACT")
      
      public class Contact implements Serializable {
      
          private static final long serialVersionUID = 1L;
          @Id
          @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;
          }
      
      }
      ContactView.fxml
      <?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">
        <children>
          <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" />
        </children>
      </AnchorPane>
      ContactViewController
      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 {
      
          @FXML
          private ResourceBundle resources;
          @FXML
          private URL location;
          @FXML
          private Button btnSaveContact;
          @FXML
          private TextField txtEmail;
          @FXML
          private TextField txtName;
          @FXML
          private TextField txtPhone;
          private Contact contact;
      
          @FXML
          void btnSaveContactAction(ActionEvent event) {
              //setting values
              contact.setName(txtName.getText());
              contact.setPhone(txtPhone.getText());
              contact.setEmail(txtEmail.getText());
              
              //saving
              EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersistenceUnit");
              EntityManager em = emf.createEntityManager();
              em.getTransaction().begin();
              em.persist(contact);
              em.getTransaction().commit();
          }
      
          @Override
          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:
      contact.setName(txtName.getText());
      
      and 
      
      txtName.setText(contact.getName());
      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?
          csh
          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.

          http://docs.oracle.com/javafx/2/api/javafx/beans/property/adapter/JavaBeanStringPropertyBuilder.html

          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().
          • 2. Re: How can I link EntityClass properties to GUI compoments?
            James_D
            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.