Javafx 8 Tableview selection with checkbox

前端 未结 2 539
迷失自我
迷失自我 2021-01-03 13:15

I\'ve set up a multiselection enabled tableview and am trying to attach a listener a checkbox inserted into a column to the selection model of the table.

che         


        
2条回答
  •  無奈伤痛
    2021-01-03 13:57

    You are using checkBoxTableColumn.setCellValueFactory incorrectly.

    Your TableView has data items of type T, and the setCellValueFactory method on a column is there to tell the column what value it must extract out of an object of type T to display.

    You however are returning an observable value containing a GUI component (CheckBox), whereas you should be returning an observable Boolean value extracted from cellData.

    See here for an Oracle tutorial on TableView: http://docs.oracle.com/javafx/2/ui_controls/table-view.htm#CJAGAAEE

    Adding a checkbox column to a table where changes to the table checkbox are propogated back to the model object is quite simple:

    TableColumn  checkCol = new TableColumn<>("Check");
    checkCol.setCellValueFactory( new PropertyValueFactory( "checkBoxValue" ) );
    checkCol.setCellFactory( CheckBoxTableCell.forTableColumn( checkCol ) );
    

    Note that "checkBoxValue" is the partial name of a property method in Job called checkBoxValueProperty() that returns a BooleanProperty. (It doesn't have to be called checkBoxValue, you can give it a different name, but it must end with Property.)

提交回复
热议问题