当前位置: 首页 > 知识库问答 >
问题:

在一列中添加两个按钮,并获得单击行值javafx

冯阳华
2023-03-14

我正在尝试在javafx的表格视图中实现一列中的两个按钮。我能够在一列中实现一个按钮,但无法在一列中添加两个按钮。我从这个链接中得到了一些想法

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

在TableView(JAVAFX)中的单元格中添加按钮

如何在TableView JavaFX的表列中添加两个按钮

我在上面的链接中使用了这个想法。然而,代码对我不起作用。

    TableColumn<Student, String> firstCol = new TableColumn<Student, String>("ID");
    TableColumn<Student, String> secondCol = new TableColumn<Student, String>("Name");
    TableColumn<Student, String> thirdCol = new TableColumn<Student, String>("Quiz Mark");
    TableColumn<Student, String> forthCol = new TableColumn<Student, String>("A1");
    TableColumn<Student, String> fifthCol = new TableColumn<Student, String>("A2");
    TableColumn<Student, String> sixthCol = new TableColumn<Student, String>("A3");
    TableColumn<Student, String> sevenCol = new TableColumn<Student, String>("Exam");
    TableColumn<Student, String> eightCol = new TableColumn<Student, String>("Result");
    TableColumn<Student, String> nineCol = new TableColumn<Student, String>("Grade");
    TableColumn<Student, Student> tenthCol = new TableColumn<Student, Student>("Action");   


firstCol.setCellValueFactory(new PropertyValueFactory<>("Id"));
    secondCol.setCellValueFactory(new PropertyValueFactory<>("Name"));
    thirdCol.setCellValueFactory(new PropertyValueFactory<>("QuizMark"));
    forthCol.setCellValueFactory(new PropertyValueFactory<>("Assingment1Mark"));
    fifthCol.setCellValueFactory(new PropertyValueFactory<>("Assingment2Mark"));
    sixthCol.setCellValueFactory(new PropertyValueFactory<>("Assingment3Mark"));
    sevenCol.setCellValueFactory(new PropertyValueFactory<>("ExamMark"));
    eightCol.setCellValueFactory(new PropertyValueFactory<>("Result"));
    nineCol.setCellValueFactory(new PropertyValueFactory<>("Grade"));
    tenthCol.setCellFactory(param -> new TableCell<Student, Student>() {
        private final Button editButton = new Button("edit");
        private final Button deleteButton = new Button("delete");
        HBox pane = new HBox(deleteButton, editButton);

        @Override
        protected void updateItem(Student patient, boolean empty) {
            super.updateItem(patient, empty);

            if (patient == null) {
                setGraphic(null);
                return;
            }

            deleteButton.setOnAction(event -> {
                Student getPatient = getTableView().getItems().get(getIndex());
                System.out.println(getPatient.getId() + "   " + getPatient.getName());
            });

            editButton.setOnAction(event -> {
                Student getPatient = getTableView().getItems().get(getIndex());
            });
            pane.getChildren().addAll(deleteButton,editButton);
            setGraphic(pane);
        }
    });

在中,不会显示“操作”列按钮。我哪里做错了。请任何人告诉我。

共有1个答案

滕无尘
2023-03-14

缺少cellValueFactory第十列中的单元格项总是null。使用updateItem方法的第二个参数来确定行是否为空。

为了说明这一点,我将列的类型更改为TableColumn

此外,每次更改项目时再次添加HBox的子级是不必要的,因为更新onAction处理程序也是不必要的,因为getIndex()是在单击Button时评估的。

您可以将代码更改为:

TableColumn<Student, Void> tenthCol = new TableColumn<>("Action");

...

tenthCol.setCellFactory(param -> new TableCell<Student, Void>() {
    private final Button editButton = new Button("edit");
    private final Button deleteButton = new Button("delete");
    private final HBox pane = new HBox(deleteButton, editButton);

    {
        deleteButton.setOnAction(event -> {
            Student getPatient = getTableView().getItems().get(getIndex());
            System.out.println(getPatient.getId() + "   " + getPatient.getName());
        });

        editButton.setOnAction(event -> {
            Student getPatient = getTableView().getItems().get(getIndex());
        });
    }

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

        setGraphic(empty ? null : pane);
    }
});

 类似资料:
  • 我想在action TableColumn中添加两个按钮,我已经阅读了如何在JavaFX表格视图中添加按钮,以及如何在表格视图(JavaFX)中的单元格中添加按钮,但这两个按钮都在中使用一个按钮,所以当我尝试使用: 它只显示了一个按钮: 我怎样才能解决这个问题?

  • 在我的程序中,它将单击浏览器中的一个按钮,并且在该页面中,应该会出现另一个按钮。出现该按钮后,我的程序将立即运行下一个操作来单击下一个按钮。我目前收到此错误: ElementNotVisibleException:消息:元素不可见 因此,我假设我正在调用该操作,以便在该按钮出现之前单击下一个按钮。我的问题是,我该怎么做才能让我的程序等到我可以点击按钮,再点击按钮? 这就是我的程序底部的代码的样子。

  • 在连续单击5次按钮后,尝试将ngClass添加到div。 这是应用程序上的按钮。组成部分html文件: 这是应用程序。组成部分ts文件: 这是一个应用程序。组成部分css 目前,在单击第5个按钮后,logItem背景保持蓝色。我可以检查控制台,看到类。白文本已经添加,但文本仍然是黑色的。 下面是它的外观: 当我检查元素时,您可以看到已经添加了类: 我对Angular是个新手,我正沿着我的路线走,被

  • 在PostgreSQL中,我想使用SQL语句合并两列并从中创建一个新列。 我正在考虑使用concat(…) ,但有更好的方法吗<最好的方法是什么?

  • 问题内容: 我有HTML表,我需要在其中选择一行并将其第一个单元格ID发送给一个按钮,而按钮的其中一个将所选值发送给Javascript函数。我该如何实现? test.html : test.js : test.css : 这是我的问题JSFIDDLE的小提琴 我需要将所选行的第一个单元格值发送到javascript函数。但是,当用户选择一行并单击“确定”按钮时,我应该将值发送给函数。这个怎么做?

  • 在上图中,我正试图点击“继续发送电子邮件”按钮。这是我的代码: 要么是我的语法错误,要么是我对网页的糟糕理解让我失望了 任何帮助-非常感谢 谢了Rob