How to populate TableView with rows of List<Object>?

In my model I have Row and Cell objects that represent tabular data read from a file. Each Row can return a list of cells. I want to populate TableView with Cell.toString() values, something like:

for (Row row : rows) {
   // Add row to TableView.
   for (Cell cell : row.getCells()) {
      // Add cell.toString() in a current TableView row.
   }
}

Most tutorials I found deal with columns that are associated with fields of some object, but I just want to display data without knowing what columns represent.


Essentially, your issue boils down to the fact that you need a dynamic amount of columns, which is not what TableView was initially made for: It does not render object relations - it was designed to display properties, which are fixed for the objects of the TableView (the keys/properties are the same for every object - eg a person's name, age, etc...)


Below I implemented a simple approach which is a fully runnable example of how this can be achieved.

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DynamicTableViewColumnCount extends Application
{

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

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        TableView<Row> tableView = new TableView<>();

        // make sample data
        List<Row> rows = makeSampleData();

        int max = getMaxCells(rows);
        makeColumns(max, tableView);
        tableView.getItems().addAll(rows);

        // Boilerplate code for showing the TableView
        Scene scene = new Scene(tableView, 1000, 1000);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void makeColumns(int count, TableView<Row> tableView)
    {
        for (int m = 0; m < count; m++)
        {
            TableColumn<Row, String> column = new TableColumn<>(Integer.toString(m));
            column.setCellValueFactory(param -> {
//                int index = Integer.parseInt(param.getTableColumn().getText());
                int index = param.getTableView().getColumns().indexOf(param.getTableColumn());
                List<Cell> cells = param.getValue().getCells();
                return new SimpleStringProperty(cells.size() > index ? cells.get(index).toString() : null);
            });
            tableView.getColumns().add(column);
        }
    }

    public int getMaxCells(List<Row> rows)
    {
        int max = 0;
        for (Row row : rows)
            max = Math.max(max, row.getCells().size());
        return max;
    }

    public List<Row> makeSampleData()
    {
        Random random = new Random();
        List<Row> rows = new ArrayList<>();
        for (int i = 0; i < 16; i++)
        {
            Row e = new Row();
            int jMax = random.nextInt(6); // from 0 to 5
            for (int j = 0; j <= jMax; j++)
            {
                e.getCells().add(new Cell(Long.toHexString(random.nextLong())));
            }
            rows.add(e);
        }
        return rows;
    }

    static class Row
    {
        private final List<Cell> list = new ArrayList<>();

        public List<Cell> getCells()
        {
            return list;
        }
    }

    static class Cell
    {
        private final String value;

        public Cell(String value)
        {
            this.value = value;
        }

        @Override
        public String toString()
        {
            return value;
        }
    }
}
链接地址: http://www.djcxy.com/p/80152.html

上一篇: 使用Map <String,Map <String,String>填充TableView JavaFX

下一篇: 如何用List <Object>的行填充TableView?