3

I am currently using JavaFX to display data from a SQL Server database. I have created a TableView based on the contents of one table in the database. The database table is a collection of Products, so the TableView declaration is:

@FXML
private TableView<Products> productTable;

And each row in the TableView is declared something like this:

@FXML
private TableColumn<Products, String> productName;

Each column has a ValueFactory which contains a PropertyValueFactory which maps the data from the database to the column. Now what I'd really like to do is have my table contain data from 2 database tables, since there is a one-to-one relationship between the product and a cost table. However, I haven't found a clever way to do this yet.

Anyone have any suggestions or idea?

2 Answers 2

2

You can write custom cell value factory, if you already defined relationships of the Products model to those table models. In the code below, the "Cost and Model of the product" column uses 2 related table models to render its cells, which are Cost and Model.

TableColumn<Person, String> costAndModelCol = new TableColumn<Person, String>("Cost and Model of the product");
costAndModelCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person, String>, ObservableValue<String>>() {
    public ObservableValue<String> call(TableColumn.CellDataFeatures<Person, String> p) {
        return SimpleStringProperty(p.getValue().getCost().getValue() + " (model: "+ p.getValue().getModel().getYear() +")");
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

So, I think what you're saying is that each cell will contain data from two different database columns? I would like to have additional columns for the data from the second table. The first 5 columns contain product specific information from my product table in the db, the next 3 columns from the cost table in the db would show cost, wholesale, retail prices. Putting them all in one tableview is what has me stumped since the TableView is based on the Product class. I also considered use a container class to hold the data from both tables, but the mechanics of CRUD to the db are difficult.
@kpenrose each cell of that column only will have a data from 2 different related tables. For last 3 columns create 3 different cell value factories. For example, replace the return line above with return SimpleStringProperty(p.getValue().getCost().getWholeSale()); in the code above, to get wholesales.
@kpenrose. If you can access to the related tables from Product table then you don't need container class. As I said even the tableview is based on Product class you can construct columns containing datas from other tables.
1

Another solution that may be applicable to your problem is to combine the data from the two tables into a JDBC connected RowSet object, and then use the jdbcRowSet object as source of data for your TableView. You can easily write an SQL query that could combine the data from both tables that you need into one rowset object.

Instead of mapping data from the database to the TableView, you'd map from the rowset object to the TableView. Naturally, this solution is available to you only if you are able to execute SQL queries against the database yourself. If you are limited to accessing the database through a data access object or other CRUD methods, then this won't work for your case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.