简体   繁体   中英

Java FX TableView not editable with FXML

I am currently developing a Java FX Application that includes a TableView . In this TableView I want to use an editable CheckBoxTableCell . First I had all components added in the Java Code using MiG Layout. With that configuration everything was editable (size of columns, order of columns, checkbox). When I transferred all the code to FXML using Scene Builder everything worked fine except the TableView. I could not figure out how to set the CellFactory and the CellValueFactory in the FXML file (I've seen a couple of examples, but couldn't get it to work). So I decided to set those factories in the initialize() method:

voteCol.setCellValueFactory(new Callback<CellDataFeatures<DataItem, Boolean>,     ObservableValue<Boolean>>() {

        @Override
        public ObservableValue<Boolean> call(
                CellDataFeatures<DataItem, Boolean> arg0) {
            return arg0.getValue().voteProperty();
        }

    });

voteCol.setCellFactory(new Callback<TableColumn<DataItem, Boolean>, TableCell<DataItem, Boolean>>() {

        @Override
        public TableCell<DataItem, Boolean> call(
                TableColumn<DataItem, Boolean> arg0) {
            return new CheckBoxTableCell<DataItem, Boolean>();
        }

    });

voteCol.setEditable(true);
dataTableView.setEditable(true);

Here is the FXML Code generated by Scene Builder

<TableView fx:id="dataTableView" editable="true"
        mouseTransparent="true" pickOnBounds="false" prefHeight="-1.0"
        prefWidth="-1.0" AnchorPane.bottomAnchor="41.0"
        AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0"
        AnchorPane.topAnchor="80.0">
        <columns>
            <TableColumn fx:id="voteCol" maxWidth="5000.0" minWidth="10.0"
                prefWidth="32.0" text="Vote" visible="true" />
            ...
        </columns>
        <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
        </columnResizePolicy>
    </TableView>

The databinding with the DataItem is working correctly.

If anyone could point me in the right direction I'd be very thankful.

Most probably your initialization is never called.

Make sure your Controller implements Initializable interface and initialize() method has correct signature:

public class MyController implements Initializable {

   @Override
   public void initialize(URL location, Resources resources) {
       //....
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM