1

我有一个 ListProperty,我在其中从数据库加载应用程序运算符列表。我想只显示在组合框中具有活动状态的运算符。

我尝试在修改 ListProperty 时对其进行过滤,但是当我仅更新操作员状态时它不起作用。我还试图找到一种将运算符列表绑定到过滤列表的方法。

有我的 2 个模型:

运算符.java

/**
 * Model representing an operator in the application.
 *
 * @author Mathieu DARÉ
 */

package com.dare.tkpf.esatraceability.model;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;





public class Operator {

//
// Attributes of the operator...
//
    private IntegerProperty                IDProperty;
    private StringProperty                 firstNameProperty;
    private StringProperty                 lastNameProperty;
    private ObjectProperty<OperatorStatus> statusProperty;
    private ObjectProperty<LocalDateTime>  dateTimeAddedProperty;
    private StringProperty                 dateTimeAddedStringProperty;





    /**
     * Constructor of the Operator.
     *
     * @param ID            The operator's ID.
     * @param firstName     The operator's first name.
     * @param lastName      The operator's last name.
     * @param status        The operator's status.
     * @param datetimeAdded The date/time of when the operator has been added.
     */
    public Operator(int ID, String firstName, String lastName, OperatorStatus status, LocalDateTime dateTimeAdded) {

        this.IDProperty                  = new SimpleIntegerProperty(ID);
        this.firstNameProperty           = new SimpleStringProperty(firstName);
        this.lastNameProperty            = new SimpleStringProperty(lastName);
        this.statusProperty              = new SimpleObjectProperty<OperatorStatus>(status);
        this.dateTimeAddedProperty       = new SimpleObjectProperty<LocalDateTime>(dateTimeAdded);
        this.dateTimeAddedStringProperty = new SimpleStringProperty( dateTimeAddedProperty.get().format( DateTimeFormatter.ofPattern("'le' dd/MM/yyyy 'à' HH:mm:ss") ) );

    }



//
// Setter, getters for the operator's ID...
//

    /**
     * Setter for the operator's ID.
     *
     * @param ID The ID of the operator.
     */
    public void setID(int ID) {

        this.IDProperty.set(ID);

    }



    /**
     * Getter for the operator's ID.
     *
     * @return The ID of the operator.
     */
    public int getID() {

        return this.IDProperty.get();

    }



    /**
     * Getter for the operator's ID property.
     *
     * @return The ID property of the operator.
     */
    public IntegerProperty IDProperty() {

        return this.IDProperty;

    }



//
// Setter, getters for the operator's first name...
//

    /**
     * Setter for the operator's first name.
     *
     * @param firstName The first name of the operator.
     */
    public void setFirstName(String firstName) {

        this.firstNameProperty.set(firstName);

    }



    /**
     * Getter for the operator's first name.
     *
     * @return The first name of the operator.
     */
    public String getFirstName() {

        return this.firstNameProperty.get();

    }



    /**
     * Getter for the operator's first name property.
     *
     * @return The first name property of the operator.
     */
    public StringProperty firstNameProperty() {

        return this.firstNameProperty;

    }



//
// Setter, getters for the operator's last name...
//

    /**
     * Setter for the operator's last name.
     *
     * @param lastName The last name of the operator.
     */
    public void setLastName(String lastName) {

        this.lastNameProperty.set(lastName);

    }



    /**
     * Getter for the operator's last name.
     *
     * @return The last name of the operator.
     */
    public String getLastName() {

        return this.lastNameProperty.get();

    }



    /**
     * Getter for the operator's last name property.
     *
     * @return The last name property of the operator.
     */
    public StringProperty lastNameProperty() {

        return this.lastNameProperty;

    }



//
// Setter, getters for the operator's status...
//

    /**
     * Setter for the operator's status.
     *
     * @param status The status of the operator.
     */
    public void setStatus(OperatorStatus status) {

        this.statusProperty.get().setID( status.getID() );
        this.statusProperty.get().setDescription( status.getDescription() );

    }



    /**
     * Getter for the operator's status.
     *
     * @return The status of the operator.
     */
    public OperatorStatus getStatus() {

        return this.statusProperty.get();

    }



    /**
     * Getter for the operator's status property.
     *
     * @return The status property of the part.
     */
    public ObjectProperty<OperatorStatus> statusProperty() {

        return this.statusProperty;

    }



    /**
     * Getter for the operator's status description.
     *
     * @return The status description of the operator.
     */
    public String getStatusDescription() {

        return this.statusProperty.get().descriptionProperty().get();

    }



    /**
     * Getter for the operator's status description property.
     *
     * @return The status description property of the part.
     */
    public StringProperty statusDescriptionProperty() {

        return this.statusProperty.get().descriptionProperty();

    }



//
// Setter, getters for the date/time the operator has been added...
//

    /**
     * Setter for the date/time the operator has been added.
     *
     * @param datetimeAdded The date/time the operator has been added.
     */
    public void setDateTimeAdded(LocalDateTime datetimeAdded) {

        this.dateTimeAddedProperty.set(datetimeAdded);

        this.dateTimeAddedStringProperty = new SimpleStringProperty( dateTimeAddedProperty.get().format( DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss") ) );

    }



    /**
     * Getter for the date/time the operator has been added.
     *
     * @return The date/time the operator has been added.
     */
    public LocalDateTime getDateTimeAdded() {

        return this.dateTimeAddedProperty.get();

    }



    /**
     * Getter for the date/time property of when the operator has been added.
     *
     * @return The date/time property of when the operator has been added.
     */
    public ObjectProperty<LocalDateTime> dateTimeAddedProperty() {

        return this.dateTimeAddedProperty;

    }



    /**
     * Getter for the date/time string property of when the operator has been added.
     *
     * @return The date/time string property of when the operator has been added.
     */
    public StringProperty dateTimeAddedStringProperty() {

        return this.dateTimeAddedStringProperty;

    }



//
// Utility methods...
//

    /**
     * Method converting the model into a string describing the operator (For direct use in the UI).
     *
     * @return The string describing the operator.
     */
    @Override
    public String toString() {

        return getFirstName() + " " + getLastName();

    }

}

OperatorStatus.java

/**
 * Model representing an operator status in the application.
 *
 * @author Mathieu DARÉ
 */

package com.dare.tkpf.esatraceability.model;


import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;





public class OperatorStatus {

//
// Attributes of the operator status...
//
    private IntegerProperty IDProperty;
    private StringProperty  descriptionProperty;





    /**
     * Constructor of the operator status.
     *
     * @param ID          The operator status' ID.
     * @param description The operator status' description.
     */
    public OperatorStatus(int ID, String description) {

        this.IDProperty          = new SimpleIntegerProperty(ID);
        this.descriptionProperty = new SimpleStringProperty(description);

    }



//
// Setter, getters for the operator status' ID...
//

    /**
     * Setter for the operator status' ID.
     *
     * @param ID The ID of the operator status.
     */
    public void setID(int ID) {

        this.IDProperty.set(ID);

    }



    /**
     * Getter for the operator status' ID.
     *
     * @return The ID of the operator status.
     */
    public int getID() {

        return this.IDProperty.get();

    }



    /**
     * Getter for the operator status' ID property.
     *
     * @return The ID property of the operator status.
     */
    public IntegerProperty IDProperty() {

        return this.IDProperty;

    }



//
// Setter, getters for the operator status' description...
//

    /**
     * Setter for the operator status' description.
     *
     * @param description The description of the operator status.
     */
    public void setDescription(String description) {

        this.descriptionProperty.set(description);

    }



    /**
     * Getter for the operator status' description.
     *
     * @return The description of the operator status.
     */
    public String getDescription() {

        return this.descriptionProperty.get();

    }



    /**
     * Getter for the operator status' description property.
     *
     * @return The description property of the operator status.
     */
    public StringProperty descriptionProperty() {

        return this.descriptionProperty;

    }



//
// Utility methods...
//

    /**
     * Method converting the operator status to string.
     *
     * @return The description of the status.
     */
    @Override
    public String toString() {

        return getDescription();

    }

}

还有我的应用程序的数据持有者,我想在其中创建操作员列表的过滤版本以在组合框中使用:

ESATraceabilityDataHolder.java

/**
 * Data holder of the application. Contains all the properties and observable data shared through the entire application.
 *
 * @author Mathieu DARÉ
 */

package com.dare.tkpf.esatraceability;


import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;

import com.dare.tkpf.esatraceability.model.NomenclatureStatus;
import com.dare.tkpf.esatraceability.model.Operator;
import com.dare.tkpf.esatraceability.model.OperatorStatus;
import com.dare.tkpf.esatraceability.model.Part;





public class ESATraceabilityDataHolder {

//
// Instance of the data holder...
//
    private static ESATraceabilityDataHolder instance = null;


//
// Reference toward the app DBMS...
//
    private static ESATraceabilityDBMS dbms = ESATraceabilityDBMS.getInstance();


//
// Properties and observable values of the application...
//
    private static ListProperty<OperatorStatus>     operatorStatuses     = null;
    private static ListProperty<Operator>           operators            = null;
    private static ListProperty<Part>               halfFinished         = null;
    private static ListProperty<NomenclatureStatus> nomenclatureStatuses = null;





//
// Constructor and base methods of the data holder...
//

    /**
     * Constructor of the data holder.
     */
    private ESATraceabilityDataHolder() {

        // Retrieval of the base data from the database at launch...
        operatorStatuses     = new SimpleListProperty<>( dbms.getOperatorsStatuses() );
        operators            = new SimpleListProperty<>( dbms.getOperators() );
        halfFinished         = new SimpleListProperty<>( dbms.getHalfFinished() );
        nomenclatureStatuses = new SimpleListProperty<>( dbms.getNomenclatureStatuses() );

    }



    /**
     * Getter of the data holder instance.
     *
     * @return Instance of the data holder.
     */
    public static ESATraceabilityDataHolder getInstance() {

        // If the data holder doesn't exist yet, we instantiate it...
        if( instance == null ) { instance = new ESATraceabilityDataHolder(); }

        return instance;

    }



//
// Getters for the shared data of the application...
//

    /**
     * Getter for the operator statuses list.
     *
     * @return The list of the operator statuses.
     */
    public ListProperty<OperatorStatus> getOperatorStatuses() {

        return operatorStatuses;

    }



    /**
     * Getter for the operators list.
     *
     * @return The list of all the operators.
     */
    public ListProperty<Operator> getOperators() {

        return operators;

    }



    /**
     * Getter for the half-finished list.
     *
     * @return The list of all the half-finished.
     */
    public ListProperty<Part> getHalfFinished() {

        return halfFinished;

    }



    /**
     * Getter for the nomenclature statuses' list.
     *
     * @return The list of the operator statuses.
     */
    public ListProperty<NomenclatureStatus> getNomenclatureStatuses() {

        return nomenclatureStatuses;

    }

}

提前致谢。

4

1 回答 1

1

首先创建一个在, 或其更改ObservableList<Operator>时触发更新通知的。这通过创建一个可观察的列表并指定一个提取器来工作:operatorStatusid

ESATraceabilityDataHolder data = ESATraceabilityDataHolder.getInstance();

ObservableList<Operator> operators = FXCollections.observableList(data.getOperators().get(),
    operator -> new Observable[] {Bindings.select(operator.statusProperty(), "ID")});

现在创建一个FilteredList过滤id状态的过滤器:

final int statusActiveId = ... ;

FilteredList<Operator> activeOperators = new FilteredList<>(operators, operator -> 
    operator.getStatus().getId() == statusActiveId );

接着

ComboBox<Operator> combo = new ComboBox<>(activeOperators);
于 2015-03-26T14:45:14.377 回答