我有一个TableView,它沿一列生成按钮:
构建任务管理应用程序。我希望能够在按下相应按钮时删除按钮所在的行,而不是{strong>不是被选中的行,如图here所示。
我的表类实现了EventHandler,因此当按下时,TableView中的按钮将运行handle()方法。
public TableList(String task, String subject, LocalDate dueDate) {
this.task = new SimpleStringProperty(task);
this.subject = new SimpleStringProperty(subject);
this.dueDate = dueDate;
this.completed = new JFXButton("Finished");
completed.setOnAction(this);
}
@Override
public void handle(ActionEvent event) {
// DELETE ROW HERE
}
唯一缺少的是如何检测按下按钮的行,然后将其删除(因为所有按钮都运行相同的handle方法)。非常感谢。
此处为清楚起见,将代码转储至https://pastebin.com/TGk4CUWh
答案 0 :(得分:2)
这里是一个演示。来自here的代码已更改。关键是在updateItem
中使用TableCell
。在Button's
onAction
中,从TableView
的当前索引处的Button
中删除。 getIndex()
获取按钮的当前索引。
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import javafx.util.Callback;
public class JustDoIt extends Application
{
private final TableView<Person> table = new TableView<>();
private final ObservableList<Person> data
= FXCollections.observableArrayList(
new Person("Jacob", "Smith"),
new Person("Isabella", "Johnson"),
new Person("Ethan", "Williams"),
new Person("Emma", "Jones"),
new Person("Michael", "Brown")
);
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage stage)
{
stage.setWidth(450);
stage.setHeight(500);
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
TableColumn actionCol = new TableColumn("Action");
actionCol.setCellValueFactory(new PropertyValueFactory<>("DUMMY"));
Callback<TableColumn<Person, String>, TableCell<Person, String>> cellFactory
= //
new Callback<TableColumn<Person, String>, TableCell<Person, String>>()
{
@Override
public TableCell call(final TableColumn<Person, String> param)
{
final TableCell<Person, String> cell = new TableCell<Person, String>()
{
final Button btn = new Button("Just Do It");
{
btn.setOnAction(event -> {
" table.getItems().remove(getIndex());
});
}
@Override
public void updateItem(String item, boolean empty)
{
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
setText(null);
}
else {
setGraphic(btn);
setText(null);
}
}
};
return cell;
}
};
actionCol.setCellFactory(cellFactory);
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, actionCol);
Scene scene = new Scene(new Group());
((Group) scene.getRoot()).getChildren().addAll(table);
stage.setScene(scene);
stage.show();
}
public static class Person
{
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private Person(String fName, String lName)
{
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
}
public String getFirstName()
{
return firstName.get();
}
public void setFirstName(String fName)
{
firstName.set(fName);
}
public String getLastName()
{
return lastName.get();
}
public void setLastName(String fName)
{
lastName.set(fName);
}
}
}