Saturday, 30 August 2014

Easy javafx table generation and population

Good day guys.
          While developing my first app in javafx, i had some difficulties which i know someone might have also encountered or is encountering. This is managing(populating and diplaying) information from a database to a table.
I used javafx scene builder to build my tables.
So i have written a class to manage these.

It contains an overloaded method which takes parameters like:

  • The query string
  • c TableColumns<SimpleString, String > 
  • TableView<SimpleString>
For instance if u want to create a table to display all users information, u simply have to call the method passing the appropriate parameters.
Example:

database -> sc
table -> st

 fname                      lname                           age
--------------------------------------------------
Fongoh                   martin                            20
Isaac                        tata                               23
Mimba                   mukasa                           20
Johnsom                Tayong                            20

/*sample method call for table generation*/

@FXML
private TableView<SimpleString> tableId;
@FXML
private TableColumn<SimpleString, String> colFname;
@FXML
private TableColumn<SimpleString, String> colLname;
@FXML
private TableColumn<SimpleString, String> colAge;


@FXML
private void populateTable( MouseEvent event ){

String query;
query = "select sc.st.fname, sc.st.lname, sc.st.age from st.sc";
/*instantiate the TableClass class*/
TableClass table = new TableClass();

/*call the makeTable method to automatically generate the table for u.*/
table.MakeTable(query, colFname, colLName, colAge, tableId);

}

The code is on github; TableInJavaFX





4 comments:

  1. Nice job Fongoh Martin we have been trying to figure out to create a class to handle tables more easily and we think this the best solution we had so far !!! thanks for the class

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Next on the list will be on using a class, that makes use of itext to make reports(write to pdf).

    ReplyDelete