首页 > 解决方案 > 如何将按钮添加到我的 JavaFX 表视图

问题描述

我正在尝试制作一个删除按钮和一个编辑按钮,但我不知道该怎么做,这是我的代码。

        TableColumn<Date, String> dateColumn = new TableColumn<>("Date");
        dateColumn.setMinWidth(75);
        dateColumn.setCellValueFactory(new PropertyValueFactory<>("Date"));

        TableColumn<Date, String> weightColumn = new TableColumn<>("Weight");
        weightColumn.setMinWidth(50);
        weightColumn.setCellValueFactory(new PropertyValueFactory<>("Weight"));

        TableColumn<Date, String> foodColumn = new TableColumn<>("Food");
        foodColumn.setMinWidth(25);
        foodColumn.setStyle("-fx-alignment: CENTER");
        foodColumn.setCellValueFactory(new PropertyValueFactory<>("Food"));

        TableColumn<Date, String> tasksColumn = new TableColumn<>("Tasks");
        tasksColumn.setMinWidth(25);
        tasksColumn.setStyle("-fx-alignment: CENTER");
        tasksColumn.setCellValueFactory(new PropertyValueFactory<>("Tasks"));

        TableColumn<Date, Button> deleteColumn = new TableColumn<>("");
        deleteColumn.setMinWidth(50);
        deleteColumn.setCellValueFactory(new PropertyValueFactory<>(""));

        TableColumn<Date, Button> editColumn = new TableColumn<>("");
        editColumn.setMinWidth(50);
        editColumn.setCellValueFactory(new PropertyValueFactory<>(""));

        dateTableView = new TableView<>();
        dateTableView.setItems(getDate());
        dateTableView.setPrefWidth(350);
        dateTableView.getColumns().setAll(dateColumn, weightColumn, foodColumn, tasksColumn,
                                   deleteColumn, editColumn);

这是生成每一行的获取日期:

 public ObservableList<Date> getDate(){
        ObservableList<Date> dates = FXCollections.observableArrayList();
        Button buttonDateDelete = new Button("Delete");
        Button buttonDateEdit = new Button("Edit");
        ArrayList<String> specificDateArray = schedule.getSpecificDateArray(fileName);
        for (int i = 0;  i < specificDateArray.size(); i++) {
            ArrayList<String> dateInfo = schedule.extractDateValues(fileName, specificDateArray.get(i));


            dates.add(new Date(dateInfo.get(0), dateInfo.get(1), makeDoneCount(dateInfo.get(2)), makeDoneCount(dateInfo.get(3)), buttonDateDelete, buttonDateEdit));
        }
        dates.add(new Date());
        return dates;
    }

我在网上搜索了代码以查看其他人是如何做到的,但我发现它非常不同且令人困惑,我尝试了一些东西但我无法让它正常工作。

标签: javajavafx

解决方案


您可以使用自定义单元格制作它

为表格定义单元格

// Localization is for support internalization
TableColumn<Project, Void> actionsCol = new TableColumn<>(Localization.getValue("tableColumn_projectActions"));
actionsCol.setMinWidth(columnWidth);
// this is Interface to get actions
actionsCol.setCellFactory(param -> new ActionsCell(this));

并定义TableCell

// for you: change Project to Date
public class ActionsCell extends TableCell<Project, Void> {

    // listener to get action of button
    private final ProjectListListener mProjectListListener;

    public ActionsCell(ProjectListListener projectListListener) {
        mProjectListListener = projectListListener;
    }

    @Override
    protected void updateItem(Void item, boolean empty) {
        super.updateItem(item, empty);

        Project parentItem = (Project) getTableRow().getItem();

        if (parentItem != null) {
            ListItemView listItemView = new ListItemView();

            listItemView.configView(parentItem, mProjectListListener);

            // set own layout item
            setGraphic(listItemView.getBox());
        }
    }

    // here is holder of cell with 2 buttons
    public static class ListItemView {

        @FXML
        private HBox hBox;

        @FXML
        private Button btnEdit;

        @FXML
        private Button btnDelete;

        public ListItemView() {
            // create fxml file for cell
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/actions.fxml"));
            Locale locale = Locale.getDefault();
            // if you support internalization
            fxmlLoader.setResources(ResourceBundle.getBundle("resources/i18n", locale));
            fxmlLoader.setController(this);
            try {
                fxmlLoader.load();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public void configView(Project project, ProjectListListener projectListListener) {
            btnEdit.setOnAction(event ->
                    // trigger listener events or write the code right here
                    projectListListener.editProject(project)
            );
            btnDelete.setOnAction(event ->
                    // trigger listener events or write the code right here
                    projectListListener.deleteProject(project)
            );
        }

        public HBox getBox() {
            return hBox;
        }
    }
}

推荐阅读