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