【问题标题】:How to make a checkbox with a selection of all checkboxes in the tableview on JavaFX and FXML?java - 如何在JavaFX和FXML的tableview中选择所有复选框来制作复选框?
【发布时间】:2019-05-09 05:29:17
【问题描述】:

大家好。遇到这样的问题。单击顶部的复选框时,有必要在表格中整个列表旁边的框中切换复选框。然后当你点击按钮时,删除选中的记录,从 MySQL 数据库中删除选中的记录。

这就是我们所做的。

PersonUnpersonValueFactoryController.java

package usersapp.controller;

import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;
import usersapp.model.Person;

public class PersonUnpersonValueFactory implements Callback<TableColumn.CellDataFeatures<Person, CheckBox>, ObservableValue<CheckBox>> {

    @Override
    public ObservableValue<CheckBox> call(TableColumn.CellDataFeatures<Person, CheckBox> param) {
        Person person = param.getValue();
        CheckBox checkBox = new CheckBox();
        checkBox.selectedProperty().setValue(person.isUnperson());

        checkBox.selectedProperty().addListener((ov, old_val, new_val) -> {
            person.setUnperson(new_val);
            System.out.println(new_val);
        });
        return new SimpleObjectProperty<>(checkBox);
    }
}

Person.java

public class Person {

private Boolean unperson;

    ...

    //unperson
    public Boolean isUnperson() {
        return this.unperson;
    }

    public void setUnperson(Boolean unperson){
        this.unperson = unperson;
    }

    ...

}

PersonView.fxml

<?import usersapp.controller.PersonUnpersonValueFactory?>

...

<TableView fx:id="personTable" editable="true" layoutX="7.0" layoutY="53.0" prefHeight="285.0" prefWidth="378.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="4.0" AnchorPane.leftAnchor="7.0" AnchorPane.rightAnchor="7.0" AnchorPane.topAnchor="53.0">
                     <columns>
                        <TableColumn prefWidth="50.0" style="-fx-alignment: CENTER;">
                           <cellValueFactory>
                              <PersonUnpersonValueFactory />
                           </cellValueFactory>
                           <graphic>
                              <CheckBox mnemonicParsing="false" />
                           </graphic>
                        </TableColumn>
                        ...
                     </columns>
                  </TableView>

非常感谢您的帮助。

【问题讨论】:

    标签: java javafx tableview fxml


    【解决方案1】:

    您只需要在“主”CheckBox 上安装一个侦听器来观察 selectedProperty 的变化。然后您可以使用它来遍历 TableView 项目并更新它们的 selectedProperty

    请注意,由于您提供的代码有限,我需要对您的数据模型的构建方式做出一些假设。

    checkBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
    
        // Loop through entire TableView to set the selected property for each Item
        for (Item item : tableView.getItems()) {
            item.setSelected(newValue);
        }
    });
    

    【讨论】:

    • 非常感谢!如果想看完整代码,可以看我的 GitHub。我的项目在这里github.com/SequelONE/UsersApp
    • Item item 是作为 Person 的模型?
    • 在这个相关的example 中,ModelHandler 说明了一种在模型更新时调节标题控件的方法。
    • @AndrejKopp - 不一定。您需要将此答案调整为您自己的模型,因为您没有在问题中包含足够的代码。恐怕 GitHub 代码的链接不擅长 SO。请查看如何为您的问题创建minimal reproducible example
    猜你喜欢
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2015-11-27
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多