package com.yezijun.aitool.text;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
import org.controlsfx.control.CheckComboBox;
public class TableViewCheckComboBox extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("TableView with CheckComboBox Cell Example");
// 创建一个TableView
TableView<Item> tableView = new TableView<>();
tableView.setEditable(true);
// 创建一个ObservableList来存储数据
ObservableList<Item> items = FXCollections.observableArrayList(
new Item("Item 1"),
new Item("Item 2"),
new Item("Item 3")
);
// 创建一个TableColumn并设置CellFactory为CheckComboBox
TableColumn<Item, String> column = new TableColumn<>("Items");
column.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
column.setCellFactory(param -> new CheckComboBoxTableCell());
// 将TableColumn添加到TableView中
tableView.getColumns().add(column);
// 设置TableView的数据模型
tableView.setItems(items);
// 创建一个Scene
Scene scene = new Scene(tableView, 300, 200);
// 设置Scene到Stage
primaryStage.setScene(scene);
// 显示Stage
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public static class Item {
private final SimpleStringProperty name;
public Item(String name) {
this.name = new SimpleStringProperty(name);
}
public String getName() {
return name.get();
}
public SimpleStringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
}
public static class CheckComboBoxTableCell extends javafx.scene.control.TableCell<Item, String> {
private final CheckComboBox<String> checkComboBox = new CheckComboBox<>();
public CheckComboBoxTableCell() {
checkComboBox.getItems().addAll("Option 1", "Option 2", "Option 3");
checkComboBox.getCheckModel().getCheckedItems().addListener((ListChangeListener<? super String>) change -> {
while (change.next()){
if (change.wasAdded()) {
System.out.println(change.getAddedSubList());
}else if(change.wasRemoved()){
System.out.println(change.getRemoved());
}
}
});
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setGraphic(null);
setText("");
} else {
checkComboBox.getCheckModel().clearChecks(); // 清除之前的选择
setGraphic(checkComboBox);
}
}
}
}
package com.yezijun.aitool.text;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
import org.controlsfx.control.CheckComboBox;
public class TableViewCheckComboBox extends Application {
}