如何在JavaFX表格视图中添加按钮

35
我在Google和Stackoverflow上搜索了相关内容,但是给出的例子都不太能理解。请问有人可以解释一下吗?
我想在表格视图的最后一列中添加一个按钮,当点击它时,应该触发监听器并传递按钮所在行的对象。我无法理解以下来自 gist.github.com 的示例:
这是我的完整代码:
public class SchermdeelWerkplaats extends BorderPane{

    //ATD moeder klasse met alle collecties etc.
    private ATD $;

    TableView tabel = new TableView();
    Button nieuwTaak = new Button("Nieuwe taak inboeken");
    final ObservableList<Task> data = FXCollections.observableArrayList();

    public SchermdeelWerkplaats(ATD a) {

        $ = a;

        data.addAll($.agenda);

        tabel.setEditable(false);
        tabel.setPlaceholder(new Label("Geen taken"));

        TableColumn c1 = new TableColumn("datum");
        c1.setMinWidth(200);
        TableColumn c2 = new TableColumn("type");
        c2.setMinWidth(100);
        TableColumn c3 = new TableColumn("uren");
        c3.setMinWidth(100);
        TableColumn c4 = new TableColumn("klaar");
        c4.setMinWidth(200);
        TableColumn c5 = new TableColumn("Werknemer");
        c5.setMinWidth(100);
        TableColumn c6= new TableColumn("Auto");
        c6.setMinWidth(400);
        TableColumn c7= new TableColumn("Actie");
        c7.setMinWidth(400);

        TableColumn col_action = new TableColumn<>("Action");

        col_action.setCellValueFactory(
                new Callback<TableColumn.CellDataFeatures<Task, Boolean>, 
                ObservableValue<Boolean>>() {

            @Override
            public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Task, Boolean> p) {
                return new SimpleBooleanProperty(p.getValue() != null);
            }
        });

        col_action.setCellFactory(
            new Callback<TableColumn<Task, Task>, TableCell<Task, Task>>() {

                @Override
                public TableCell<Task, Task> call(TableColumn<Task, Task> p) {
                    return new ButtonCell();
                }
            }
        );

        c1.setCellValueFactory(
            new PropertyValueFactory<Task,Date>("date")
        );
        c2.setCellValueFactory(
            new PropertyValueFactory<Task,Task.TaskType>("type")
        );
        c3.setCellValueFactory(
            new PropertyValueFactory<Task,Double>("hours")
        );
        c4.setCellValueFactory(
            new PropertyValueFactory<Task,Boolean>("done")
        );
        c5.setCellValueFactory(
            new PropertyValueFactory<Task,Employee>("employee")
        );
        c6.setCellValueFactory(
            new PropertyValueFactory<Task,Car>("car")
        );

        tabel.getColumns().addAll(c1, c2, c3, c4, c5, c6, c7);
        tabel.setItems(data);

        setCenter(tabel);
        setBottom(nieuwTaak);

    }

    //letterlijk van internet geplukt en datatype aangepast
    private class ButtonCell extends TableCell<Task, Task> {


        private Button cellButton;

        ButtonCell(){
              cellButton = new Button("jjhjhjh");
            cellButton.setOnAction(new EventHandler<ActionEvent>(){

                @Override
                public void handle(ActionEvent t) {
                    // do something when button clicked
                    Task record = getItem();
                    // do something with record....
                }
            });
        }

        //Display button if the row is not empty
        @Override
        protected void updateItem(Task record, boolean empty) {
            super.updateItem(record, empty);
            if(!empty){
                cellButton.setText("Something with "+record);
                setGraphic(cellButton);
            } else {
                setGraphic(null);
            }
        }
    }

}

现在我需要创建一个ButtonCell extends TableCell,这部分是可以理解的。但如何将其分配给列呢?

我明白这一点:

c1.setCellValueFactory(
        new PropertyValueFactory<Task,Date>("date")
    );

但不是这样的:
           TableColumn col_action = new TableColumn<>("Action");

            col_action.setCellValueFactory(
                    new Callback<TableColumn.CellDataFeatures<Task, Boolean>, 
                    ObservableValue<Boolean>>() {

                @Override
                public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Task, Boolean> p) {
                    return new SimpleBooleanProperty(p.getValue() != null);
                }
            });

            col_action.setCellFactory(
                new Callback<TableColumn<Task, Task>, TableCell<Task, Task>>() {

                    @Override
                    public TableCell<Task, Task> call(TableColumn<Task, Task> p) {
                        return new ButtonCell();
                    }
                }
            );
2个回答

67
为了能够渲染列,TableColumn 需要 cellValueFactory。但是“动作”列在基础数据模型中不存在。在这种情况下,我只需给cellValueFactory赋一个虚拟值并继续进行:
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");

                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                            setText(null);
                        } else {
                            btn.setOnAction(event -> {
                                Person person = getTableView().getItems().get(getIndex());
                                System.out.println(person.getFirstName()
                                        + "   " + person.getLastName());
                            });
                            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);
        }

    }
}

谢谢,我现在明白了,也感谢你的示例,现在我已经把它搞定了。 - botenvouwer
getTableView().getItems().get(getIndex()); 不是一个函数,我尝试添加了但不起作用。 - Dante Cervantes
任何使用JFXTreeTableView的人,在按钮点击事件中使用以下代码: User user = getTreeTableView().getTreeItem(getIndex()).getValue(); - Martin Mbae

23
这是我的示例,使用了很棒的Java 8功能并扩展了TableCell类。
让我快速解释一下我在做什么: 我创建了一个ActionButtonTableCell类,它扩展了TableCell。然后,您可以使用Java 8 lambda函数为按钮创建操作。
import java.util.function.Function;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;

public class ActionButtonTableCell<S> extends TableCell<S, Button> {

    private final Button actionButton;

    public ActionButtonTableCell(String label, Function< S, S> function) {
        this.getStyleClass().add("action-button-table-cell");

        this.actionButton = new Button(label);
        this.actionButton.setOnAction((ActionEvent e) -> {
            function.apply(getCurrentItem());
        });
        this.actionButton.setMaxWidth(Double.MAX_VALUE);
    }

    public S getCurrentItem() {
        return (S) getTableView().getItems().get(getIndex());
    }

    public static <S> Callback<TableColumn<S, Button>, TableCell<S, Button>> forTableColumn(String label, Function< S, S> function) {
        return param -> new ActionButtonTableCell<>(label, function);
    }

    @Override
    public void updateItem(Button item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
            setGraphic(null);
        } else {                
            setGraphic(actionButton);
        }
    }
}

接下来的实现非常简单,这是一个示例按钮,用于从表格中删除项目:

column.setCellFactory(ActionButtonTableCell.<Person>forTableColumn("Remove", (Person p) -> {
    table.getItems().remove(p);
    return p;
}));

根据评论更新: 尽管使用在TableCell中使用Button的方法违反了推荐的设计原则,但在某些情况下,如果下面提到的缺点对您的用例不是重要的问题,仍然可以使用:

  1. 关注点分离的违规:将UI控件与数据表示类混合在一起会使代码的模块化程度降低,维护变得更加困难。

  2. 灵活性有限:直接在TableCell中嵌入Button会限制按钮和单元格本身的自定义选项,使得实现复杂交互或自定义样式变得困难。

  3. 内存使用效率低:每个带有Button的TableCell实例都会消耗额外的内存,在拥有许多行的大型TableView中可能会引发问题。

  4. UI控件焦点:将Button放置在TableCell中可能会将焦点从底层数据转移到UI控件,可能影响用户体验。

考虑到这些缺点,通常建议遵循推荐的设计原则,将数据表示和UI控件的关注点分开。然而,如果这些缺点对于您特定的用例不重要,并且您更喜欢原始方法的简单性,您可以继续使用。


1
很好的解决方案。我很好奇observable扮演了什么角色。 - Brad Turek
1
这是为了将来参考,但对于此答案可以删除,因为它没有被使用。我会更新答案。谢谢。 - Darrel K.
1
这个答案不太好,应该避免使用。引用James_D的话说,“TableView,TableColumn,TableCell等应该是数据类型,而不是UI控件类型。因此,你永远不应该有像TableCell <S,Button>这样的情况。Button不是数据类型。”。https://dev59.com/4VR_sogBPY-HTNNjFi6t - SedJ601

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接