简体   繁体   English

如何在 JavaFx Tablecolumn 标题中放置复选框

[英]How do I place a checkbox in a JavaFx Tablecolumn header

I cannot find an example of how I place a checkbox in a JavaFX Tablecolumn header.我找不到如何在 JavaFX Tablecolumn 标题中放置复选框的示例。 I have the checkbox and handler all ready to go.我已经准备好了复选框和处理程序。 Does anyone know how?有谁知道怎么做? James_D?詹姆斯_D? :) :)

You'll want to use the TableColumnBase#graphic property.您需要使用TableColumnBase#graphic属性。 The graphic of a column will be displayed in the column's header.列的图形将显示在列的标题中。 The property's value type is Node which means it can be any arbitrary UI object, including CheckBox .该属性的值类型是Node ,这意味着它可以是任意的 UI 对象,包括CheckBox

Here's a proof-of-concept :这是一个概念验证

import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.stage.Stage;

public class Main extends Application {

  @Override
  public void start(Stage primaryStage) {
    var table = new TableView<Item>();

    for (int i = 1; i <= 25; i++) {
      table.getItems().add(new Item("Item #" + i));
    }

    var selecteAllCheckBox = new CheckBox();
    selecteAllCheckBox.setOnAction(
        event -> {
          event.consume();
          table.getItems().forEach(item -> item.setSelected(selecteAllCheckBox.isSelected()));
        });

    var selectedCol = new TableColumn<Item, Boolean>();
    selectedCol.setGraphic(selecteAllCheckBox);
    selectedCol.setSortable(false);
    selectedCol.setPrefWidth(50);
    selectedCol.setCellValueFactory(data -> data.getValue().selectedProperty());
    selectedCol.setCellFactory(CheckBoxTableCell.forTableColumn(selectedCol));
    table.getColumns().add(selectedCol);

    var nameCol = new TableColumn<Item, String>("Name");
    nameCol.setPrefWidth(550);
    nameCol.setCellValueFactory(data -> data.getValue().nameProperty());
    table.getColumns().add(nameCol);

    primaryStage.setScene(new Scene(table, -1, 400));
    primaryStage.show();
  }

  static class Item {

    // -- PROPERTIES --

    private final BooleanProperty selected = new SimpleBooleanProperty();
    final void setSelected(boolean selected) { this.selected.set(selected); }
    final boolean isSelected() { return selected.get(); }
    final BooleanProperty selectedProperty() { return selected; }

    private final StringProperty name = new SimpleStringProperty();
    final void setName(String name) { this.name.set(name); }
    final String getName() { return name.get(); }
    final StringProperty nameProperty() { return name; }

    // -- CONSTRUCTOR --

    Item(String name) {
      setName(name);
    }
  }
}

Note: This example does not listen to the selection status of the table items and will not update the column's CheckBox when an item is (de)selected.注意:此示例不侦听表项的选择状态,也不会在(取消)选择项时更新列的CheckBox For instance, a more complete example would mark the column's CheckBox as selected if the user manually selects every item.例如,如果用户手动选择每个项目,则更完整的示例会将列的CheckBox标记为已选中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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