How can I add rows and columns to a JavaFX 8 TableView

Victor Grazi

I see examples on the internet for adding a row to a TableView, for example using the Person class in the Oracle documentation.

But I have a variable number of columns, so I can't bind to a Person (or any other) bean business object.

The Oracle example goes on to show how to bind columns to property names, but for that, it only shows how to add columns, but not rows.

My question is, can someone point me to a Hello, World example of dynamically adding arbitrary columns and/or rows to a JavaFX 8 TableView?

James_D

Use a List<String> (for example) for the data type, and just set the cell value factory as a callback that indexes into the list.

For example, this will create a TableView<List<String>> that is constructed out of an arbitrary tab-delimited text file. Not all rows in the file need have the same number of elements (it will pad with blanks). (It doesn't support escaped tabs, etc):

public TableView<List<String>> readTabDelimitedFileIntoTable(Path file) throws IOException {
    TableView<List<String>> table = new TableView<>();
    Files.lines(file).map(line -> line.split("\t")).forEach(values -> {
        // Add extra columns if necessary:
        for (int i = table.getColumns().size(); i < values.length; i++) {
            TableColumn<List<String>, String> col = new TableColumn<>("Column "+(i+1));
            col.setMinWidth(80);
            final int colIndex = i ;
            col.setCellValueFactory(data -> {
                List<String> rowValues = data.getValue();
                String cellValue ;
                if (colIndex < rowValues.size()) {
                    cellValue = rowValues.get(colIndex);
                } else {
                     cellValue = "" ;
                }
                return new ReadOnlyStringWrapper(cellValue);
            });
            table.getColumns().add(col);
        }

        // add row:
        table.getItems().add(Arrays.asList(values));
    });
    return table ;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Can I group rows in a Javafx TableView?

From Dev

Can I group rows in a Javafx TableView?

From Dev

JavaFX: add rows at the beginning of the TableView

From Dev

Javafx how can I insert data to tableview

From Dev

How to add new Rows to TableView in JavaFX, based on a sorted filtered List?

From Dev

How can I determine the X & Y coordinates of a row in a TableView in JavaFX 8

From Dev

How can I get each rows of tableview?

From Dev

How can I get each rows of tableview?

From Dev

How to add a tooltip to a TableView header cell in JavaFX 8

From Dev

How can i add menubutton on tableview header

From Dev

How to dynamically add data to the tableView only in the last rows without disturbing other rows with observavbleList in javafx?

From Dev

How can I select multiple cells in tableview with javafx only by mouse?

From Dev

How can I solve this visual glitch in my JavaFX TableView?

From Dev

How can I right click on a cell in tableview in Javafx (fxml)?

From Dev

How can I add an audioclip in Javafx?

From Dev

how can i store selected rows of tableview in nsuserdefaults in swift 3

From Dev

How can I transfer multiple rows of a tableView to another ViewController

From Dev

How can I dynamically add table rows

From Dev

How can I transform some columns into rows with other columns repeated?

From Dev

How can I add strings on the first 2 columns and add buttons on the 3rd column for each rows of strings in pyqt tablewidget?

From Dev

How can I add columns in a data frame?

From Dev

how can I add to columns in R

From Dev

how can I add to columns in R

From Dev

JavaFX I can´t fill tableview by observableList

From Dev

How to hide TableView column header in JavaFX 8?

From Dev

How to add a click event to a tableview cell in javafx

From Dev

How to add an Image into a JavaFx TableView column

From Dev

JavaFX TableView how to add items to column?

From Dev

Duplicate columns in JavaFX TableView

Related Related

  1. 1

    Can I group rows in a Javafx TableView?

  2. 2

    Can I group rows in a Javafx TableView?

  3. 3

    JavaFX: add rows at the beginning of the TableView

  4. 4

    Javafx how can I insert data to tableview

  5. 5

    How to add new Rows to TableView in JavaFX, based on a sorted filtered List?

  6. 6

    How can I determine the X & Y coordinates of a row in a TableView in JavaFX 8

  7. 7

    How can I get each rows of tableview?

  8. 8

    How can I get each rows of tableview?

  9. 9

    How to add a tooltip to a TableView header cell in JavaFX 8

  10. 10

    How can i add menubutton on tableview header

  11. 11

    How to dynamically add data to the tableView only in the last rows without disturbing other rows with observavbleList in javafx?

  12. 12

    How can I select multiple cells in tableview with javafx only by mouse?

  13. 13

    How can I solve this visual glitch in my JavaFX TableView?

  14. 14

    How can I right click on a cell in tableview in Javafx (fxml)?

  15. 15

    How can I add an audioclip in Javafx?

  16. 16

    how can i store selected rows of tableview in nsuserdefaults in swift 3

  17. 17

    How can I transfer multiple rows of a tableView to another ViewController

  18. 18

    How can I dynamically add table rows

  19. 19

    How can I transform some columns into rows with other columns repeated?

  20. 20

    How can I add strings on the first 2 columns and add buttons on the 3rd column for each rows of strings in pyqt tablewidget?

  21. 21

    How can I add columns in a data frame?

  22. 22

    how can I add to columns in R

  23. 23

    how can I add to columns in R

  24. 24

    JavaFX I can´t fill tableview by observableList

  25. 25

    How to hide TableView column header in JavaFX 8?

  26. 26

    How to add a click event to a tableview cell in javafx

  27. 27

    How to add an Image into a JavaFx TableView column

  28. 28

    JavaFX TableView how to add items to column?

  29. 29

    Duplicate columns in JavaFX TableView

HotTag

Archive