vendredi 13 mai 2016

How to design columns & model for string/bool values in TableView

I have to create a TableView in JavaFX for which the number of columns are known only at runtime. The first column is a string column, while the second to the last column are checkboxes. Only at runtime I know how many checkbox columns I have (but I always have exactly 1 string column, the first one). For example, if I had 2 checkbox columns, I could do:

    TableColumn attributeColumn = new TableColumn("Attribute");
    attributeColumn.setCellValueFactory(new PropertyValueFactory<test, String>("name"));        
    attributeTable.getColumns().add(attributeColumn);       

    TableColumn<CompanyGroup, Boolean> col1 = new TableColumn<>("1");
    col1.setCellValueFactory(new PropertyValueFactory<>("group1"));
    col1.setCellFactory(CheckBoxTableCell.forTableColumn(col1));
    col1.setEditable(true);
    attributeTable.getColumns().addAll(col1);

    TableColumn<CompanyGroup, Boolean> col2 = new TableColumn<>("2");
    col2.setCellValueFactory(new PropertyValueFactory<>("group2"));
    col2.setCellFactory(CheckBoxTableCell.forTableColumn(col2));
    col2.setEditable(true);
    attributeTable.getColumns().addAll(col2);

Now to support many checkbox columns at runtime, I could follow this approach: How to populate TableView with rows of List<Object>?. But this approach assumes only one type of object (string), while I have (String, Boolean, Boolean, Boolean, ...). The result should look like this:

enter image description here

So I have these 2 questions: 1) How can I add dynamic checkbox columns and 2) How can I specify the model behind this table? Thank you very much!




Aucun commentaire:

Enregistrer un commentaire