2014-03-03 9 views
0

我打算創建desktop application。在我的應用程序中,服務器和客戶端上的數據庫只是請求服務器獲取數據並返回相應類的對象。如何將數據感知到JavaFX的TableView中

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

現在我想用這個學生對象來填充TableView

我想填充數據到TableView使用簡單的字符串對象,我不能這樣做?我不想使用SimpleStringPropery,我想使用String對象。 我使用休眠,所以我映射我所有的類

+0

列表學生= sess.createQuery(「FROM Student」)。list();現在我想用這個對象來感受TableView。那麼我該如何做到這一點。 –

+0

創建'ObservalbleList '而不是'List '。更新我的答案! – ItachiUchiha

+1

您當然可以在表列而不是StringProperties中使用字符串。假設您的Student類具有其屬性的getXXX()方法,只需使用PropertyValueFactory作爲TableColumns上的CellValueFactory即可。但是,如果您希望表格是可編輯的,那麼您將需要做一些額外的工作。 –

回答

1

只需創建一個ObservableList<Student>並將其與您的表綁定!簡單!

您可以從ObservableList<Student>List<Student>

List<Student> students = sess.createQuery("FROM Student").list(); 
ObservableList<Student> listStudents = FXCollections.observableList(students); 
TableView table = new TableView(); 
table.setItems(listStudents); 

您必須添加TableColumnTableView,對於一個完整的例子,你可以通過

http://docs.oracle.com/javafx/2/ui_controls/table-view.htm#CJABHGAJ

相關問題