有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javafx从TableView中的按钮列删除行[Java]

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

My program

构建任务管理应用程序。当按下相应的按钮时,我希望能够删除按钮所在的行,而不是选中的行,如图here

My table类实现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


共 (1) 个答案

  1. # 1 楼答案

    这是一个演示。更改了here中的代码。关键是在TableCell中使用updateItem。在Button's{}中,从TableView的当前索引处删除ButtongetIndex()获取当前索引中的按钮

    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);
            }
    
        }
    }