How to add button in JavaFX table view

后端 未结 2 1957
猫巷女王i
猫巷女王i 2020-11-30 04:38

I have searched at Google and Stackoverflow for this and I just don\'t get the given examples. Can someone please explain it to me.

I want to add a button to the la

相关标签:
2条回答
  • 2020-11-30 05:07

    To be able to render the column, TableColumn needs cellValueFactory. But the "action" column does not exist in underlying data model. In this case, I just give some dummy value to cellValueFactory and move on:

    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);
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-11-30 05:16

    Here is my example using awesome Java 8 Functionality and extending TableCell class.

    Let me give a quick explanation of what I am doing: I created a ActionButtonTableCell class that extends TableCell. And then you can use java 8 lamda functions to create an Action for the button.

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

    The implementation is then as simple as this, This is a sample button to remove the item from the table:

    column.setCellFactory(ActionButtonTableCell.<Person>forTableColumn("Remove", (Person p) -> {
        table.getItems().remove(p);
        return p;
    }));    
    
    0 讨论(0)
提交回复
热议问题