将复选框添加到TableView列

时间:2019-08-09 11:48:03

标签: listview javafx boolean checkboxlist

我正在重建javafx桌面应用程序,以便应用新功能并从原始应用程序中删除无用的代码。特别是带有两列的过滤后的列表视图:名称和复选框。

到目前为止,我无法为每个行成功创建复选框,我只能在第二列中显示字符串值“ false / true”。

将第二列使用的方法从字符串更改为布尔值后,我开始遇到编译PersonTableController.class的问题...

Person.class

package com.genx;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.SimpleBooleanProperty;

public class Person {

    public SimpleStringProperty firstName;
    public SimpleBooleanProperty selected;

    public Person(String firstName, boolean selected) {
        this.firstName = new SimpleStringProperty(firstName);
        this.selected = new SimpleBooleanProperty(selected);
    }

    @SuppressWarnings("unused")
    public String getFirstName() {
       return firstName.get();
    }

    @SuppressWarnings("unused")
    public boolean getSelected() {
       return selected.get();
    }

    @SuppressWarnings("unused")
    public SimpleBooleanProperty selectedProperty() {
      return selected;
    }
}

PersonTablecontroller.class

package com.genx;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.text.Text;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;


/**
 * View-Controller for the person table.
 */
public class PersonTableController {

    @FXML
    Button generate;
    @FXML
    Text status;
    @FXML
    TextField textVorname;
    @FXML
    TextField textNachname;
    @FXML
    TextField textUsergroup;
    @FXML
    TextField adname;
    @FXML
    CheckBox checkbox;
    @FXML
    CheckBox checkbox2;
    @FXML
    TextField filterField;
    @FXML
    TableView<Person> personTable = new TableView<Person>();
    @FXML   
    TableColumn<Person, String> firstNameColumn = new TableColumn<>("Roles Names");
    @FXML   
    TableColumn<Person, Boolean> lastNameColumn = new TableColumn<>("Selected");


    /**
     * Just add some sample data in the constructor.
     */
        ObservableList<Person> masterData = FXCollections.observableArrayList(
new Person("Hans", false), new Person("Ruth", false), new Person("Heinz", false), new Person("Cornelia", false), new Person("Werner", false), new Person("Lydia", false), new Person("Anna", false), new Person("Susan", true), new Person("Joan", false));

@FXML
private void initialize() {
// 0. Initialize the columns.
    personTable.setEditable(true);
    firstNameColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
    lastNameColumn.setCellValueFactory(new PropertyValueFactory<>("selected"));
 lastNameColumn.setCellFactory(CheckBoxTableCell.forTableColumn(lastNameColumn));
    lastNameColumn.setEditable(true);
    personTable.getColumns().addAll(firstNameColumn, lastNameColumn);
    personTable.setItems(masterData);

    // 1. Wrap the ObservableList in a FilteredList (initially display all data).
    FilteredList<Person> filteredData = new FilteredList<>(masterData, p -> true);

    // 2. Set the filter Predicate whenever the filter changes.
    filterField.textProperty().addListener((observable, oldValue, newValue) -> {
            filteredData.setPredicate(person -> {
                // If filter text is empty, display all persons.
                if (newValue == null || newValue.isEmpty()) {
                    return true;
                }

                // Compare first name and last name of every person with filter text.
                String lowerCaseFilter = newValue.toLowerCase();

                if (person.getFirstName().toLowerCase().indexOf(lowerCaseFilter) != -1) {
                    return true; // Filter matches first name.
                }
                return false; // Does not match.
            });
        });
        // 3. Wrap the FilteredList in a SortedList. 
        SortedList<Person> sortedData = new SortedList<>(filteredData); 
        // 4. Bind the SortedList comparator to the TableView comparator.       sortedData.comparatorProperty().bind(personTable.comparatorProperty());

        // 5. Add sorted (and filtered) data to the table.
        personTable.setItems(sortedData);
    }

    public void generateImpex() {
        Alert warning = new Alert(AlertType.WARNING, "Generating Impex!", ButtonType.YES, ButtonType.CANCEL);
        warning.showAndWait();
    }
}

在当前状态下,由于某种原因无法找到Person.class,因此我无法编译控制器。

如果在Person.class中将Boolean属性和方法更改为String,那么我可以使用此代码对PersonTableController.class进行编译:

package com.genx;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.text.Text;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;


/**
 * View-Controller for the person table.
 */
public class PersonTableController {

    @FXML
    Button generate;
    @FXML
    Text status;
    @FXML
    TextField textVorname;
    @FXML
    TextField textNachname;
    @FXML
    TextField textUsergroup;
    @FXML
    TextField adname;
    @FXML
    CheckBox checkbox;
    @FXML
    CheckBox checkbox2;
    @FXML
    private TextField filterField;
    @FXML
    private TableView<Person> personTable;
    @FXML
    private TableColumn<Person, String> firstNameColumn;
    @FXML
    private TableColumn<Person, String> lastNameColumn; // TableColumn<Person, Boolean> lastNameColumn;

    private ObservableList<Person> masterData = FXCollections.observableArrayList();

    public PersonTableController() {
        masterData.add(new Person("Hans", "false"));
        masterData.add(new Person("Ruth", "false"));
        masterData.add(new Person("Heinz", "false"));
        masterData.add(new Person("Cornelia", "false"));
        masterData.add(new Person("Werner", "false"));
        masterData.add(new Person("Lydia", "false"));
        masterData.add(new Person("Anna", "false"));
        masterData.add(new Person("Stefan", "false"));
        masterData.add(new Person("Martin", "false"));
        masterData.add(new Person("Joni", "false"));
        masterData.add(new Person("Chachi", "false"));
        masterData.add(new Person("Phillip", "false"));
        masterData.add(new Person("Susan", "true"));
        masterData.add(new Person("Joan", "false"));
    }

    /**
     * Initializes the table columns and sets up sorting and filtering.
     */
    @FXML
    private void initialize() {
        // 0. Initialize the columns.
        firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
        lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
        // 1. Wrap the ObservableList in a FilteredList (initially display all data).
        FilteredList<Person> filteredData = new FilteredList<>(masterData, p -> true);
        // 2. Set the filter Predicate whenever the filter changes.
        filterField.textProperty().addListener((observable, oldValue, newValue) -> {
            filteredData.setPredicate(person -> {
                // If filter text is empty, display all persons.
                if (newValue == null || newValue.isEmpty()) {
                    return true;
                }
                String lowerCaseFilter = newValue.toLowerCase();            
                if (person.getFirstName().toLowerCase().indexOf(lowerCaseFilter) != -1) {
                    return true; // Filter matches first name.
                }
                return false; // Does not match.
            });
        });
        // 3. Wrap the FilteredList in a SortedList. 
        SortedList<Person> sortedData = new SortedList<>(filteredData);

        // 4. Bind the SortedList comparator to the TableView   sortedData.comparatorProperty().bind(personTable.comparatorProperty());

        // 5. Add sorted (and filtered) data to the table.
        personTable.setItems(sortedData);
    }

    public void generateImpex() {
        Alert warning = new Alert(AlertType.WARNING, "Generating Impex!", ButtonType.YES, ButtonType.CANCEL);
        warning.showAndWait();
    }
}

0 个答案:

没有答案