Pular para o conteúdo principal

Showing Object Properties in a TableView

This post is not to talk about any specific app but to show a simple How-To with TableView.
 Show values in a table in JavaFX is easy. You can show an object property with a few lines of code since we have classes that allows you to produce cells that read directly from the object. For example, let's say we have a class Person that contains two properties:
 

public class Person {
 private String name;
 private int id;
       //getters, setters..
}
If you want to create to columns to show the id and name properties, you need to create two columns and inform each column a PropertyValueFactory object instance:
TableView tableView = new TableView();
TableColumn idColumn = new TableColumn("Id");
idColumn.setCellValueFactory(new PropertyValueFactory("id"));
TableColumn nameColumn = new TableColumn("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory("name"));
tableView.getColumns().addAll(idColumn, nameColumn);
Let's complicate a bit! What's if my object Person contains other object, for example, City and we want to show a City property? If we try to add a column for the property city with the following code:
 
TableColumn cityColumn = new TableColumn("City");
cityColumn.setCellValueFactory(new PropertyValueFactory("city"));

But it will not show any city property, it will actually show the toString method return:

Obviously you can go to City class code and override this method. Let't get the things a bit more complicated. This time let's say that you need to show a property from a City property, for example, State.

Person - City - State

Well, you can read things in the method toString of the object City, but this is not the best solution. One alternative is to set a CellValueFactory in your column using a Callback class.
. With this approach we can read any property we want from the Person class and return it to be showed in the table. It's pretty easy to use it:
TableColumn stateColumn = new TableColumn("State");
stateColumn.setCellValueFactory(new Callback, ObservableValue>() {    
    @Override    
    public ObservableValue call(CellDataFeatures c) {
        return new SimpleStringProperty(c.getValue().getCity().getState().getName());
    }
});

That's basically it what I wanted to show in this post. The entire test application code:

package org.jesuino.javafx.tableview.cellfactory;

import java.util.Arrays;
import java.util.List;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.SceneBuilder;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPaneBuilder;
import javafx.stage.Stage;
import javafx.util.Callback;

import org.jesuino.javafx.tableview.cellfactory.model.City;
import org.jesuino.javafx.tableview.cellfactory.model.Person;
import org.jesuino.javafx.tableview.cellfactory.model.State;

public class Main extends Application {

 TableView tableViewRef;

 public static void main(String[] args) {
  launch();
 }

 @Override
 public void start(Stage stage) throws Exception {

  stage.setScene(SceneBuilder
    .create()
    .width(420)
    .height(200)
    .root(StackPaneBuilder.create()
      .children(tableViewRef = createTableView()).build())
    .build());
  stage.setTitle("Testing Table View Cell Factory");
  stage.show();

  tableViewRef.getItems().addAll(mockPersonItems());
 }

 @SuppressWarnings("unchecked")
 private TableView createTableView() {
  TableView tableView = new TableView();

  TableColumn idColumn = new TableColumn(
    "Id");
  idColumn.setCellValueFactory(new PropertyValueFactory(
    "id"));
  idColumn.setMinWidth(80);

  TableColumn nameColumn = new TableColumn(
    "Name");
  nameColumn
    .setCellValueFactory(new PropertyValueFactory(
      "name"));
  nameColumn.setMinWidth(150);

  TableColumn cityColumn = new TableColumn(
    "City");
  cityColumn
    .setCellValueFactory(new PropertyValueFactory(
      "city"));
  cityColumn.setMinWidth(150);

  TableColumn stateColumn = new TableColumn(
    "State");
  stateColumn
    .setCellValueFactory(new Callback, ObservableValue>() {
     @Override
     public ObservableValue call(
       CellDataFeatures c) {
      return new SimpleStringProperty(c.getValue().getCity()
        .getState().getName());
     }
    });

  tableView.getColumns().addAll(idColumn, nameColumn, cityColumn,
    stateColumn);

  return tableView;
 }

 private List mockPersonItems() {
  State sp = new State("São Paulo", "SP");

  City saoJoseDosCampos = new City(sp, "São José dos Campos");
  City taubate = new City(sp, "Taubaté");
  City jacarei = new City(sp, "Jacareí");

  Person p1 = new Person("William Antônio Siqueira", 1, saoJoseDosCampos);
  Person p2 = new Person("Maria", 2, saoJoseDosCampos);
  Person p3 = new Person("João", 3, taubate);
  Person p4 = new Person("Joana", 4, jacarei);

  return Arrays.asList(p1, p2, p3, p4);
 }
}

Comentários

  1. Thanks for this post. Like the Javadoc it addresses the readonly case only, unfortunately. In other words, if person.name changes it is surprisingly difficult to update the tableview. Apparently the old name is cached by PropertyValueFactory, without any obvious way to invalidate it...

    ResponderExcluir
  2. deepc, I have a work in progress addressing that problem. I created several custom components to display java POJO object in TableView. Take a look at http://panemu.com/articles/?p=73&lang=en
    The sourcecode is available

    ResponderExcluir
  3. Regarding the problem of the TableView not updating automatically when the underlying data changes, I found a simple solution on "Updating rows in Tableview" http://stackoverflow.com/questions/10912690/updating-rows-in-tableview

    Check the answer from June 6 / 2012. The solution is simply to wrap the relevent fields of the class that holds the data inside Property classes and then providing getters for those properties. Example:

    private final SimpleStringProperty name = SimpleStringProperty("");

    public SimpleStringProperty nameProperty() {
    return name;
    }

    public String getName() {
    return name.getValue();
    }

    public void setName(String v) {
    name.setValue(v);
    }


    Regarding the code in this blog, I could not understand the <"persons string="string"> type specification (and neither my Java compiler :D)

    Julian

    ResponderExcluir

Postar um comentário

Postagens mais visitadas deste blog

Creating custom Work Item Handler in BPM Suite/jBPM 6

Hi all! In this post I am going to share my experience creating a work item handler for BPM Suite 6.0.3 (which is very similar to jbpm as explained in a previous post ). The Hello World Work Item Handler Let's create a really simple WorkItemHandler just to print "Hello World" in the console. Of course you can implement more feature and make it receive parameters, access database, web services, etc. But in this post we will keep it simple. Really simple. First thing to do is to create a maven project for my WorkItemHandler. Here's the pom.xml of my Maven project: Notice that we are importing jbpm dependency so we can find the java interface that is used to create work item handlers. Now we can start coding. Let's create a class name HelloWorkItemHandler in package org.jugvale.jbpm, here is the simple code: Now we can build our project and go to BPM Suite to configure it so we can use this workitem. Registering the WorkItemHandler UPDATE...

Using the REST Task from jBPM 6

In this post I am going to share my experience using the BPM Suite 6 REST Task. BPM Suite  X jBPM jBPM is a JBoss community project to offer an open source and efficient BPM Suite and BPM Suite is a product created by Red Hat based on jBPM. In this post I will be using BPM Suite because I have it installed already, but what I am going to described should work for jBPM as well.   REST Task  The REST Task is used to invoke a REST resource (or perform an HTTP Request) from your Business Process. You can set the URL, HTTP Method, timeout and credentials from the Process Modeller interface and when the process gets into that task, it will generate a HTTP Request and return the response as String. It is available on the left side pallet under the "Service Tasks" tab: The Target REST Web Service The service we are going to access from our process is a really simple one, that's why it is called simple-hello-rs and it is based on JBoss helloworld-rs qu...

Simplest JavaFX ComboBox autocomplete

Based on this Brazilian community post , I've created a sample Combobox auto complete. What it basically does is: When user type with the combobox selected, it will work on a temporary string to store the typed text; Each key typed leads to the combobox to be showed and updated If backspace is type, we update the filter Each key typed shows the combo box items, when the combobox is hidden, the filter is cleaned and the tooltip is hidden:   The class code and a sample application is below. I also added the source to my personal github , sent me PR to improve it and there are a lot of things to improve, like space and accents support.