gpt4 book ai didi

java - 自定义 TableView 以具有 "Add"按钮

转载 作者:行者123 更新时间:2023-12-01 09:05:20 26 4
gpt4 key购买 nike

我的 fxml 中有一个 TableView 作为复杂 POJO 的容器。

我已成功自定义显示,为所述复杂 POJO 的每个实例提供一个漂亮的 Pane ,但现在我想编辑底层集合。

为此,我可以在我的 View 中添加一个带有 Action 监听器和yadda yadda的按钮。

但这有点不连贯,我更喜欢将添加按钮直接放在我的 TableView 上。最好是在右侧的“标题栏”之类的地方。

我用 GridPane 做了类似的事情,但不想完全重构我的代码,所以这里是我想要实现的效果的屏幕截图:

最上面的行是表格标题,因此样式略有不同。我错过了一些非常简单的事情吗?

最佳答案

这个答案是基于这个gist :

对于标题中的 + 按钮,您只需创建一个 TableColumn 并将其图形设置为按钮。

Button addButton = new Button("+"); 
TableColumn<Person, Object> buttonColumn = new TableColumn<>();
buttonColumn.setGraphic(addButton);

对于每行中的-按钮,您需要自定义TableCell类的updateItem方法以允许每行中都有按钮。为了处理每行按钮的操作,我们使用CallBack:

public class ButtonTableCell<S,T> extends TableCell<S,T> {

private Button button;

public ButtonTableCell(final Callback<Integer, Void> pressedCallback) {
this(pressedCallback, null, null);
}

public ButtonTableCell(final Callback<Integer, Void> pressedCallback, String buttonText, Node buttonGraphic) {
this.button = new Button(buttonText, buttonGraphic);
this.button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
pressedCallback.call(getTableRow().getIndex());
}
});
}

@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if(empty) {
setGraphic(null);
} else {
setGraphic(button);
button.disableProperty().bind(Bindings.not(
getTableView().editableProperty().and(
getTableColumn().editableProperty()).and(
editableProperty())
));
}
}
}

然后使按钮列的 CellFactory 使用此修改后的 TableCell:

public class TableViewWithButtonColumnDemo extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
TableView<Person> tableview = new TableView<>(
FXCollections.observableArrayList(
new Person("Person", "1"),
new Person("Person", "2"),
new Person("Person", "3")));

TableColumn<Person, String> firstNameColumn = new TableColumn<>("First Name");
firstNameColumn.setCellValueFactory(new PropertyValueFactory("firstName"));
TableColumn<Person, String> lastNameColumn = new TableColumn<>("Last Name");
lastNameColumn.setCellValueFactory(new PropertyValueFactory("lastName"));

Button addButton = new Button("+");
addButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
tableview.getItems().add(new Person("person", String.valueOf(tableview.getItems().size()+1)));
}
});

TableColumn<Person, Object> buttonColumn = new TableColumn<>();
buttonColumn.setGraphic(addButton);
buttonColumn.setCellFactory(new Callback<TableColumn<Person,Object>, TableCell<Person,Object>>() {
@Override
public TableCell<Person, Object> call(TableColumn<Person, Object> param) {
Callback<Integer, Void> pressedCallback = new Callback<Integer, Void>() {
@Override
public Void call(Integer index) {
Person buttonPressedPerson = tableview.getItems().get(index);
tableview.getItems().remove(buttonPressedPerson);
return null;
}
};
return new ButtonTableCell<>(pressedCallback, "-", null);
}
});

tableview.setEditable(true);
tableview.getColumns().addAll(firstNameColumn, lastNameColumn, buttonColumn);

primaryStage.setScene(new Scene(tableview));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

结果:

enter image description here

关于java - 自定义 TableView 以具有 "Add"按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41324095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com