Patel Nik
Patel Nik

Reputation: 520

how to feel data into TableView in JavaFX

I'm going to create desktop application. In my application the database on the server and client just request to server to fetch data and return the object of the appropriate class.

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

Now I want to use this students object to fill the TableView.

I want fill data into TableView using simple String object, cant I do this ?

I don't want to use SimpleStringPropery, I want to use String object. I'm using hibernate so I mapping my all classes

Upvotes: 0

Views: 313

Answers (1)

ItachiUchiha
ItachiUchiha

Reputation: 36792

Just create an ObservableList<Student> and bind it with your table ! Simple !

You can make an ObservableList<Student> from your List<Student>

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

You will have to add TableColumn to the TableView, for a complete example, you can go through

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

Upvotes: 1

Related Questions