java - 从 TableView 内的按钮列中删除行 [Java]

标签 java javafx tableview javafx-8 action

我有一个 TableView,它沿着一列生成按钮:

My program

构建任务管理应用程序。我希望能够删除按钮所在的行,而不是选定的行,如图 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
}

唯一缺少的是如何检测按下按钮的行,然后将其删除(因为所有按钮都运行相同的句柄方法)。非常感谢您的帮助。

为清楚起见,此处转储代码:https://pastebin.com/TGk4CUWh

最佳答案

这是一个演示。更改代码 here 。关键是在TableCell中使用updateItem。在 ButtononAction 中,从 TableViewButton 的当前索引处删除。 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);
        }

    }
}

关于java - 从 TableView 内的按钮列中删除行 [Java],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53342065/

相关文章:

带有 EJB 配置的 Java JNDI

java - JTable 不会跨越 JFrame 的边界

JavaFX Controller FXMLLoader.load(getClass().getResource());

swift - tableView 中屏幕外的单元格在访问时返回 nil, swift

Java、Apache POI、从 MS Excel 中查看时单元格值不会改变

java - Spring Kafka Consumer 如何跳过 Avro Deserializer 异常

java - 将 CheckComboBox 添加到 PropertySheet JavaFX

JavaFx 如何隐藏/显示 TreeItem

java - 如何向 ComboBoxTableCell 添加键盘编辑支持

java - 如何根据 TableView 中的行元素自定义表格单元格/表格列内容(JavaFX)