我有一个包含3列带有复选框的tableview。选中一个复选框后,行中的其他复选框将被禁用。我有一个带有3个BooleanProperty的模型类(Pizza),它们与这些复选框链接在一起。 我写了这段代码,但这是错误的,因为当选中一个复选框时,它禁用了一行,并且无法撤消它。加上带有表视图中模型项的observableList。代码:
private ObservableList<Pizza> finalizePizzaOb = FXCollections.observableArrayList(); cl_lowSize.setCellFactory(CheckBoxTableCell.forTableColumn(param -> {
if (finalizePizzaOb.get(param).isLowSize()) {
row.disableProperty().bind(finalizePizzaOb.get(param).lowSizeProperty().isNotEqualTo(finalizePizzaOb.get(param).mediumSizeProperty().or(
finalizePizzaOb.get(param).largeSizeProperty())));
}
return finalizePizzaOb.get(param).lowSizeProperty();
}));
cl_mediumSize.setCellFactory(CheckBoxTableCell.forTableColumn(param -> {
if (finalizePizzaOb.get(param).isMediumSize()) {
row.disableProperty().bind(finalizePizzaOb.get(param).mediumSizeProperty().isNotEqualTo(finalizePizzaOb.get(param).lowSizeProperty().or(
finalizePizzaOb.get(param).largeSizeProperty())));
}
return finalizePizzaOb.get(param).mediumSizeProperty();
}));
cl_largeSize.setCellFactory(CheckBoxTableCell.forTableColumn(param -> {
if (finalizePizzaOb.get(param).isLargeSize()) {
row.disableProperty().bind(finalizePizzaOb.get(param).largeSizeProperty().isNotEqualTo(finalizePizzaOb.get(param).mediumSizeProperty().or(
finalizePizzaOb.get(param).lowSizeProperty())));
}
return finalizePizzaOb.get(param).largeSizeProperty();
}));
谢谢!
答案 0 :(得分:1)
此示例应用程序的表格视图包含三列-其中两列为复选框。当选中一列中的复选框时,另一个中的复选框未被选中,反之亦然。根据发布的问题,这可以应用于带有复选框的三列或更多列。
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.control.*;
import javafx.scene.control.cell.*;
import javafx.geometry.Insets;
import javafx.collections.*;
import java.util.*;
import javafx.beans.property.*;
public class TableCheckboxesExample extends Application {
@Override public void start(Stage primaryStage) {
TableView<Pizza> table = new TableView<>();
table.setEditable(true);
table.setItems(getDataRoutine());
TableColumn<Pizza, String> titleCol = new TableColumn<Pizza, String>("Title");
titleCol.setCellValueFactory(new PropertyValueFactory<Pizza, String>("title"));
TableColumn<Pizza, Boolean> meatCol = new TableColumn<Pizza, Boolean>("Meat");
meatCol.setEditable(true);
meatCol.setCellValueFactory(new PropertyValueFactory<Pizza, Boolean>("meat"));
meatCol.setCellFactory(CheckBoxTableCell.forTableColumn(meatCol));
TableColumn<Pizza, Boolean> vegCol = new TableColumn<Pizza, Boolean>("Veg");
vegCol.setEditable(true);
vegCol.setCellValueFactory(new PropertyValueFactory<Pizza, Boolean>("veg"));
vegCol.setCellFactory(CheckBoxTableCell.forTableColumn(vegCol));
table.getColumns().addAll(titleCol, meatCol, vegCol);
VBox vbox = new VBox();
vbox.setPadding(new Insets(25, 25, 25, 25));
vbox.getChildren().addAll(table);
Scene scene = new Scene(vbox, 350, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("Pizza table with checkboxes");
primaryStage.show();
table.getSelectionModel().select(0);
}
private ObservableList<Pizza> getDataRoutine() {
List<Pizza> pizzas = Arrays.asList(new Pizza("Meat lover"),
new Pizza("Veggie lover"), new Pizza("Meditarranean"),
new Pizza("Italian"));
ObservableList<Pizza> data = FXCollections.observableArrayList();
data.addAll(pizzas);
// Add listeners to boolean properties
for (Pizza pizza : data) {
pizza.meatProperty().addListener((obs, wasSelected, isSelected) -> {
pizza.setVeg(wasSelected);
});
pizza.vegProperty().addListener((obs, wasSelected, isSelected) -> {
pizza.setMeat(wasSelected);
});
}
return data;
}
public static void main(String[] args) {
launch(args);
}
public class Pizza {
private SimpleBooleanProperty veg;
private SimpleBooleanProperty meat;
private SimpleStringProperty title;
public Pizza(String title) {
this.veg = new SimpleBooleanProperty(false);
this.meat = new SimpleBooleanProperty(false);
this.title = new SimpleStringProperty(title);
}
public boolean isVeg() {
return veg.get();
}
public void setVeg(boolean value) {
veg.set(value);
}
public SimpleBooleanProperty vegProperty() {
return veg;
}
public boolean isMeat() {
return meat.get();
}
public void setMeat(boolean value) {
meat.set(value);
}
public SimpleBooleanProperty meatProperty() {
return meat;
}
public String getTitle() {
return title.get();
}
public void setTitle(String value) {
this.title.set(value);
}
public SimpleStringProperty titleProperty() {
return title;
}
}
}
答案 1 :(得分:0)
您可能需要在此处做一些“修改”。
cl_mediumSize.setCellFactory(new CheckBoxTableCell<Pizza, Boolean>() {
@Override public void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty); // Must keep this
Pizza model = getTableRow().getItem();
CheckBox cb = (CheckBox) getGraphic();
if (model == null) return;
cb.disableProperty().bind(
Bindings.not(
getTableView().editableProperty().and(
getTableColumn().editableProperty()).and(
editableProperty()))
.and(
model.lowSizeProperty().or(
model.largeSizeProperty()))
);
}
});