【问题标题】:javafx - checkboxes in tableview when one is selected others in row are disabledjavafx-选拔当一个选择的其他人被禁用的其他其他人时
【发布时间】:2018-07-12 13:32:13
【问题描述】:

我有一个带有 3 列和复选框的表格视图。当一个复选框被选中时,行中的其他复选框被禁用。我有一个带有 3 个 BooleanProperty 的模型类(Pizza),它们与这些复选框链接。 我写了这段代码,但这是错误的,因为当一个复选框被选中并且无法撤消它时,它会禁用一行。加上带有来自 tableview 的模型项的 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();
                }));

谢谢!!

【问题讨论】:

标签: java javafx tableview


【解决方案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;
        }
    }
}

【讨论】:

  • OP 希望其他复选框禁用,而不是未选中。
【解决方案2】:

你可能需要在这里做一些“hack”。

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()))
            );
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 2023-03-11
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 2014-04-25
    相关资源
    最近更新 更多