meriley
meriley

Reputation: 1881

JavaFX Table not populating

I'm having an issue populating a JavaFX Table View. I have the correct amount of rows and columns but my strings are not being displayed in the table. This is the method in the extended TableView class.

public void buildTable() {
    ArrayList<Phy> phy = manager.getPhy();
    ObservableList<ObservableList> list = FXCollections.observableArrayList();

    TableColumn v = new TableColumn("Virt");
    v.setMinWidth(100);

    TableColumn p = new TableColumn("Phy");
    p.setMinWidth(100);

    this.getColumns().addAll(v, p);

    int size = 0;
    for (Phy pp : phy) {
        ObservableList<String> row = FXCollections.observableArrayList();
        size = pp.getVirt().getFunc().size() > size ? pp.getVirt().getFunc().size() : size;

        row.add(pp.getVirt().getName());
        row.add(pp.getName());
        for (Func func : pp.getVirt().getFunc()) {
            row.add(func.getName());
        }
        list.add(row);
    }

    for (int i = 0; i < size; i++) {
        TableColumn obj = new TableColumn("Func " + i);
        obj.setMinWidth(100);
        this.getColumns().add(obj);
    }

    this.setItems(list);
}

My Rows are of variable length. Could this be an issue?

This table seems to be slow to display as well. When I click on the Tab in which I have this embedded it takes a few seconds to display.

Thanks

Upvotes: 1

Views: 213

Answers (1)

SnakeDoc
SnakeDoc

Reputation: 14441

Always! update your GUI components from a GUI thread!

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        // code to update GUI elements
    }
});

Platform.runLater() essentially adds your statement to a queue, which gets executed in a non-blocking fashion to update the GUI elements from inside a GUI thread. If you try updating GUI elements from non-GUI threads, sometimes it may work, others it wont, sometimes it will give weird threading exceptions, etc.

Also, there is no need to have 2 ObservableList's. Just use one, and insert a row of data at a time.

You may find this example helpful: https://github.com/SnakeDoc/superD/blob/master/src/com/vanomaly/superd/controller/MainWindowController.java

Line 186 starts the method that adds data to the table. As you can see, it uses the Platform.runLater() as well as a static method from inside the controller class (part of MVC pattern).

Table and column setup is at 64-68, and Column setup (for special data types, formatting, etc) is at 133-163.

Upvotes: 1

Related Questions