【问题标题】:get selected checkboxs from tableview从 tableview 中获取选定的复选框
【发布时间】:2016-06-28 23:13:35
【问题描述】:

我在每一行都有带有复选框的表格视图,并且我有一个操作按钮。我的问题是,如何从 tableview 中选择复选框以在按下按钮时应用操作?

这就是我将复选框添加到表格视图的方式

    public void addCeckBoxToTableView() {
    /** define a simple boolean cell value for the action column so that the column will only be shown for non-empty rows. */
    tcCb.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Object, Boolean>,
            ObservableValue<Boolean>>() {
        @Override
        public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Object, Boolean> p) {
            return new SimpleBooleanProperty(p.getValue() != null);
        }
    });
    /** create a cell value factory with an add button for each row in the table. */
    tcCb.setCellFactory(new Callback<TableColumn<Object, Boolean>, TableCell<Object, Boolean>>() {
        @Override
        public TableCell<Object, Boolean> call(TableColumn<Object, Boolean> p) {
            return new CheckBoxCell();
        }
    });
}

    private class CheckBoxCell extends TableCell<Object, Boolean> {
    CheckBox checkBox = new CheckBox();
    HBox hb = new HBox(checkBox);
    /**
     * places button in the row only if the row is not empty.
     */
    @Override
    protected void updateItem(Boolean item, boolean empty) {
        super.updateItem(item, empty);
        if (!empty) {
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
            setGraphic(hb);
        } else {
            setGraphic(null);
        }
    }
}

诚挚的。

【问题讨论】:

标签: intellij-idea javafx javafx-2 javafx-8


【解决方案1】:

我和詹姆斯的观点相同:“用那种设置你不能”

但你可以这样做

private TableView<Record> table;
private TableColumn<Record, Boolean> tcCb;
private Button actionButton;

public class Record {
    private SimpleBooleanProperty selected = new SimpleBooleanProperty();

    public SimpleBooleanProperty selectedProperty() {
        return selected;
    }
}

@Override
public void start(Stage primaryStage) throws Exception {
    table = new TableView<Record>(FXCollections.observableArrayList(new Record(), new Record(), new Record()));
    table.setEditable(true);
    // Create "CheckBox" - Column
    tcCb = new TableColumn<Record, Boolean>("Boolean-Column");
    tcCb.setCellValueFactory(new PropertyValueFactory<Record, Boolean>("selected"));
    tcCb.setCellFactory(CheckBoxTableCell.forTableColumn(tcCb));
    tcCb.setEditable(true);
    table.getColumns().add(tcCb);
    // Create actionButton for retrieving cellData
    actionButton = new Button("action");
    actionButton.setOnAction(actionEvent -> {
        for (int row = 0; row < table.getItems().size(); row++) {
            System.out.println(tcCb.getCellData(row));
        }
    });
    // The uninteresting stuff...
    primaryStage.setScene(new Scene(new VBox(table, actionButton)));
    primaryStage.show();
}

【讨论】:

    【解决方案2】:

    UI 元素应该将它们与对象相关联。在您的示例中,您希望对选定的项目应用操作(选定复选框)。

    与表格关联的Object可以是这样的

    public class TableData{
    private Boolean selected=Boolean.False;
    public void setSelected(Boolean isSelected){
    this.isSelected = isSelected;
    }
    
    public boolean isSelected(){
    return this.selected;
    }
    }
    

    所以在 TableCell 中, 选中复选框时,通过向 CheckBox 添加选择操作侦听器来更新 TableData 的“selected”布尔值。

    然后您可以遍历 TableData,您可以从 TableView 中获取它,以在按钮选择时应用操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-30
      • 2021-10-07
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多