简体   繁体   中英

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. I have the checkbox and handler all ready to go. Does anyone know how? James_D? :)

You'll want to use the TableColumnBase#graphic property. 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 .

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. For instance, a more complete example would mark the column's CheckBox as selected if the user manually selects every item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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