Rafael Wörner
Rafael Wörner

Reputation: 319

Populate TableView with not observable data

I'm really new to java and javaFX but I need this stuff to accomplish an obligatory exercise.

So far I coded a console program for importing csv values and make some comparisons on the values. But now the task is to create a GUI for the same stuff. So I want to use the same code coded for CSVimport and comparison for the GUI, but I don't know for example how to populate a TableView (TableColumn) with my data which is not coded as StringProperty or DoubleProperty etc. All Tutorials which I found (for example here or here) use those Properties. So how can I use regular Strings or doubles to populate a table?

Upvotes: 0

Views: 491

Answers (1)

James_D
James_D

Reputation: 209684

If you have a POJO such as

public class Person {

  // ...

  public String getName() { 
      return name ;
  }

  // ...
}

Then you can just do

TableView<Person> table = new TableView<>();
TableColumn<Person, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
    @Override
    public ObservableValue<String> call(CellDataFeatures<Person, String> cellData) {
        return new ReadOnlyStringWrapper(cellData.getValue().getName());
    }
});

In JavaFX 8, the last statement becomes

nameColumn.setCellValueFactory(cellData -> 
    new ReadOnlyStringWrapper(cellData.getValue()));

Upvotes: 2

Related Questions