我尝试使用FXML在tableview中创建复选框单元格。复选框显示但未激活。单击它不会改变其状态。 这是我的FXML文件
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.collections.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<?import sample.*?>
<?import javafx.geometry.Insets?>
<?scenebuilder-preview-i18n-resource BasicApplication_en.properties?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="sample.Controller">
<children>
<MenuBar VBox.vgrow="NEVER">
<menus>
<Menu mnemonicParsing="false" text="%menu.title.file">
<items>
<MenuItem mnemonicParsing="false" text="%menu.title.new" />
<MenuItem mnemonicParsing="false" text="%menu.title.open" />
<Menu mnemonicParsing="false" text="%menu.title.open.recent" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="%menu.title.close" />
<MenuItem mnemonicParsing="false" text="%menu.title.save" />
<MenuItem mnemonicParsing="false" text="%menu.title.save.as" />
<MenuItem mnemonicParsing="false" text="%menu.title.revert" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="%menu.title.preferences" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="%menu.title.quit" onAction="#closeApp"/>
</items>
</Menu>
<Menu mnemonicParsing="false" text="%menu.title.edit">
<items>
<MenuItem mnemonicParsing="false" text="%menu.title.undo" />
<MenuItem mnemonicParsing="false" text="%menu.title.redo" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="%menu.title.cut" />
<MenuItem mnemonicParsing="false" text="%menu.title.copy" />
<MenuItem mnemonicParsing="false" text="%menu.title.paste" />
<MenuItem mnemonicParsing="false" text="%menu.title.delete" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="%menu.title.select.all" />
<MenuItem mnemonicParsing="false" text="%menu.title.unselect.all" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="%menu.title.help">
<items>
<MenuItem mnemonicParsing="false" text="%menu.title.about" />
</items>
</Menu>
</menus>
</MenuBar>
<ToolBar>
<items>
<Button mnemonicParsing="false" text="%toolbar.button.add" />
<Button mnemonicParsing="false" text="%toolbar.button.edit" />
<Button mnemonicParsing="false" text="%toolbar.button.duplicate" onAction="#duplicateAction"/>
</items>
</ToolBar>
<TableView fx:id="tableView" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" >
<columns>
<TableColumn fx:id="enabledColumn" prefWidth="75.0" text="%tableview.enabled">
<cellValueFactory>
<PropertyValueFactory property="enabled" />
</cellValueFactory>
<cellFactory>
<CheckBoxCellFactory>
</CheckBoxCellFactory>
</cellFactory>
</TableColumn>
<TableColumn fx:id="timeColumn" prefWidth="75.0" text="%tableview.time">
</TableColumn>
<TableColumn fx:id="descriptionColumn" prefWidth="75.0" text="%tableview.description">
<cellValueFactory>
<PropertyValueFactory property="description" />
</cellValueFactory>
<cellFactory>
<FormattedTableCellFactory alignment="left">
</FormattedTableCellFactory>
</cellFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<MyTimer enabled="false" description="desc1"/>
<MyTimer enabled="true" description="desc2"/>
</FXCollections>
</items>
</TableView>
</children>
<stylesheets>
<URL value="@BasicApplication.css" />
</stylesheets>
</VBox>
这是我的CheckBoxCellFactory.java
package sample;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.util.Callback;
public class CheckBoxCellFactory<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
@Override
public TableCell<S, T> call(TableColumn<S, T> p) {
CheckBoxTableCell<S, T> cell = new CheckBoxTableCell<S, T>(){
private ObservableValue<T> ov;
@Override
public void updateItem(Object item, boolean empty) {
super.updateItem((T) item, empty);
if (item == null) {
super.setText(null);
super.setGraphic(null);
}
else {
if (ov instanceof BooleanProperty) {
super.selectedStateCallbackProperty().unbindBidirectional((ObjectProperty) ov);
}
ov = getTableColumn().getCellObservableValue(getIndex());
if (ov instanceof BooleanProperty) {
super.selectedStateCallbackProperty().bindBidirectional((ObjectProperty) ov);
}
}
}
};
cell.setAlignment(Pos.CENTER);
return cell;
}
}
我做错了什么?
答案 0 :(得分:0)
public class TestCase extends RecursiveTreeObject<TestCase> {
// SimpleBooleanProperty
private SimpleBooleanProperty isSelected;}
TreeTableColumn<TestCase, Boolean> selectColumn = new TreeTableColumn<>("Select");
selectColumn.setCellFactory(CheckBoxTreeTableCell.forTreeTableColumn(selectColumn));
selectColumn.setCellValueFactory(param -> {
return param.getValue().getValue().getIsSelected();
});
selectColumn.setOnEditCommit((CellEditEvent<TestCase, Boolean> t) -> {
((TestCase) t.getTreeTableView().getTreeItem(t.getTreeTablePosition().getRow()).getValue()).getIsSelected()
.set(t.getNewValue());
// refreshTable(t);
});
`