2

I want to display in TableView, data from a class "Etudiant" we call the method "show()" to fetch data into the tableview.

The class Etudiant has String properties, not SimpleStringProperty

So how can I populate my tableView ? Controller:

@FXML
TableView<Enseignant> tableau;

@FXML
TableColumn ID;

@FXML
TableColumn<Enseignant, String> NOM;

@FXML
TableColumn PRENOM;

@FXML
ComboBox<String> matiereCB;

void show(){     
    ObservableList<Enseignant> e = FXCollections.observableArrayList();
//em : EnseignantModel : the model of this class
//show : the method that brings data in a vector
    Vector<Enseignant> enseignants = em.show();
    Enseignant enseignant = new Enseignant();
    for (int i = 0; i < enseignants.size(); i++) {
        enseignant = enseignants.get(i);
        e.add(enseignant);                        
    }
    tableau.setItems(e);
}

FXML:

<TableView fx:id="tableau" layoutX="387.0" layoutY="177.0" prefHeight="361.0" prefWidth="734.0">
    <columns>
        <TableColumn fx:id="ID" prefWidth="43.0" text="ID" />
        <TableColumn fx:id="NOM" prefWidth="95.0" text="NOM" />
        <TableColumn fx:id="PRENOM" prefWidth="94.0" text="PRENOM" />
        <TableColumn fx:id="MATIERE" prefWidth="97.0" text="Matière" />
    </columns>
</TableView>

Enseignant:

public class Enseignant  implements java.io.Serializable {

   private Integer idEnseignant;
   private String nom;
   private String prenom;
1
  • 1
    What is the issue? I can see that you haven't set the cellValueFactory for the columns. Commented Nov 14, 2015 at 23:59

1 Answer 1

4

You should set cellValueFactory for each column in your table.

public class EnseignantFxmlController implements Initializable {

    @FXML
    private TableView<Enseignant> tableau;
    @FXML
    private TableColumn ID;
    @FXML
    private TableColumn NOM;
    @FXML
    private TableColumn PRENOM;


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        //each cellValueFactory has been set according to the member variables of your entity class
        ID.setCellValueFactory(new PropertyValueFactory<Enseignant, Integer>("idEnseignant"));
        NOM.setCellValueFactory(new PropertyValueFactory<Enseignant, String>("nom"));
        PRENOM.setCellValueFactory(new PropertyValueFactory<Enseignant, String>("prenom"));
        tableau.setItems(getEnseignant());
    }

  //here you can add all your Enseignants into a ObservableList
    public ObservableList<Enseignant> getEnseignant() {
        ObservableList<Enseignant> enseignantList = FXCollections.observableArrayList();
        Session session = util.HibernateUtil.
                getSessionFactory().openSession();
        List<Enseignant> eList = session.
                createCriteria(Enseignant.class).list();
        for (Enseignant ent : eList) {
            enseignantList.add(ent);
        }
        return enseignantList;
    }

  }
Sign up to request clarification or add additional context in comments.

1 Comment

then should type of the fields of entity class be string or stringproperty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.