如何在JavaFX中将数据导入TableView

如何在JavaFX中将数据导入TableView,javafx,javafx-2,Javafx,Javafx 2,我将创建桌面应用程序。在我的应用程序中,服务器和客户端上的数据库只是请求服务器获取数据并返回相应类的对象 public void serveStudentList() { BufferedOutputStream bos = null; ObjectOutputStream oos = null; Session sess = null; Transaction tx = null; try { bos = new BufferedOutp

我将创建
桌面应用程序
。在我的应用程序中,服务器和客户端上的数据库只是请求服务器获取数据并返回相应类的对象

public void serveStudentList() {
    BufferedOutputStream bos = null;
    ObjectOutputStream oos = null;
    Session sess = null;
    Transaction tx = null;
    try {
        bos = new BufferedOutputStream(con.getOutputStream());
        oos = new ObjectOutputStream(bos);

        sess = HUtil.getSessionFactory().openSession();
        tx = sess.beginTransaction();
        sess = HUtil.getSessionFactory().openSession();

        @SuppressWarnings("unchecked")
        List<Student> students = sess.createQuery("FROM Student").list();

        tx.commit();

        System.out.println("DATABASE query complete.\nWriting Object of List type to the Sream.");

        oos.writeObject(students);
        oos.flush();
        System.out.println("Object Wrote...\n Closing this stream now.");
        oos.close();
        bos.close();
        con.close();


    } catch (IOException e) {
        e.printStackTrace();
    } catch (HibernateException he) {
        if (tx != null) {
            tx.rollback();
        }
        he.printStackTrace();
    } finally {
        sess.close();
    }
public void serveStudentList(){
BufferedOutputStream bos=null;
ObjectOutputStream oos=null;
会话sess=null;
事务tx=null;
试一试{
bos=新的BufferedOutputStream(con.getOutputStream());
oos=新对象输出流(bos);
sess=HUtil.getSessionFactory().openSession();
tx=sess.beginTransaction();
sess=HUtil.getSessionFactory().openSession();
@抑制警告(“未选中”)
List students=sess.createQuery(“来自学生”).List();
tx.commit();
System.out.println(“数据库查询完成。\n正在将列表类型的对象写入Sream”);
oos.writeObject(学生);
oos.flush();
System.out.println(“对象写入…\n立即关闭此流”);
oos.close();
bos.close();
con.close();
}捕获(IOE异常){
e、 printStackTrace();
}捕获(冬眠异常he){
如果(tx!=null){
tx.回滚();
}
he.printStackTrace();
}最后{
sess.close();
}
现在,我想使用这个students对象填充
表视图

我想使用简单的字符串对象将数据填充到TableView中,我不能这样做吗

我不想使用
simplestringproperty
,我想使用
String
对象。
我正在使用hibernate,所以我映射我的所有类

只需创建一个
可观察列表
,并将其与您的表绑定!简单

您可以从
列表中创建
可观察列表

List students=sess.createQuery(“来自学生”).List();
ObservableList学生=FXCollections.ObservableList(学生);
TableView table=新TableView();
表2.setItems(学生列表);
您必须将
TableColumn
添加到
TableView
,作为完整的示例,您可以通过


List students=sess.createQuery(“FROM Student”).List();现在我想用这个对象来感受TableView。那么我该怎么做呢?创建一个
ObservalbleList
而不是
List
。更新我的答案!你当然可以在表列中使用字符串而不是StringProperties。假设你的学生类有getXXX()方法的属性,只需使用PropertyValueFactory作为TableColumns上的CellValueFactory。但是,如果希望该表可编辑,则需要做一些额外的工作。
List<Student> students = sess.createQuery("FROM Student").list();
ObservableList<Student> listStudents = FXCollections.observableList(students);
TableView table = new TableView();
table.setItems(listStudents);