javaFX FXML,使用Hibernate填充TableView

时间:2015-11-14 23:55:46

标签: hibernate javafx tableview fxml

我想在TableView中显示来自班级的数据" Etudiant " 我们称该方法为" show ()"将数据提取到tableview中。

Etudiant 类具有字符串属性, SimpleStringProperty

那么如何填充我的tableView? 控制器:

@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 个答案:

答案 0 :(得分:3)

您应该为表格中的每一列设置cellValueFactory

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

  }