Java TableView没有';t更新所有列

Java TableView没有';t更新所有列,java,uitableview,user-interface,javafx,tableview,Java,Uitableview,User Interface,Javafx,Tableview,我试图通过制作简单的应用程序进入JavaFX。现在我正在研究一种叫做“员工数据库”的东西,它意味着将一些数据存储在表和树中,其中树按部门对员工进行排序。当您点击菜单栏中的“添加工作人员”标签时,hbox将显示,在归档其文本字段并单击“添加”按钮后,表格中应显示书面数据。问题是TableView更新了除“工作年限”和“部门”列之外的所有列。我读了一些关于cannonical变量名正确使用valuefactory的技巧,但我仍然无法克服这一点 public class Main extends Ap

我试图通过制作简单的应用程序进入JavaFX。现在我正在研究一种叫做“员工数据库”的东西,它意味着将一些数据存储在表和树中,其中树按部门对员工进行排序。当您点击菜单栏中的“添加工作人员”标签时,hbox将显示,在归档其文本字段并单击“添加”按钮后,表格中应显示书面数据。问题是TableView更新了除“工作年限”和“部门”列之外的所有列。我读了一些关于cannonical变量名正确使用valuefactory的技巧,但我仍然无法克服这一点

public class Main extends Application {

WTable table = new WTable();
WTree tree = new WTree();
WMenu menu = new WMenu();
WTextField addFirstName = new WTextField("First Name");
WTextField addSecondName = new WTextField("Second Name");
WTextField addLastName = new WTextField("Last Name");
WTextField addDep = new WTextField("Department");
WTextField addYears = new WTextField("Years Worked");
WTextField addSalary = new WTextField("Salary");
WTextField addAge = new WTextField("Age");
WButton addButton = new WButton("Add");
ObservableList<Model> mList = FXCollections.observableArrayList();
HBox addBox = new HBox();


private class WTable extends TableView<Model> {

    private TableColumn<Model, String> fNameCol;
    private TableColumn<Model, String> lNameCol;
    private TableColumn<Model, String> sNameCol;
    private TableColumn<Model, String> depCol;
    private TableColumn<Model, String> yearCol;
    private TableColumn<Model, String> salaryCol;
    private TableColumn<Model, String> ageCol;


    public WTable() {
        super();
        fNameCol = new TableColumn<Model, String>("First Name");
        fNameCol.setMinWidth(100);
        fNameCol.setCellValueFactory(
                 new PropertyValueFactory<Model, String>("firstName"));

        sNameCol = new TableColumn<Model, String>("Second Name");
        sNameCol.setMinWidth(100);
        sNameCol.setCellValueFactory(
                new PropertyValueFactory<Model, String>("secondName"));

        lNameCol = new TableColumn<Model, String>("Last Name");
        lNameCol.setMinWidth(100);
        lNameCol.setCellValueFactory(
                new PropertyValueFactory<Model, String>("lastName"));



        depCol = new TableColumn<Model, String>("Department");
        depCol.setMinWidth(100);
        depCol.setCellValueFactory(
                new PropertyValueFactory<Model, String>("department"));

        yearCol = new TableColumn<Model, String>("Years Worked");
        yearCol.setMinWidth(100);
        yearCol.setCellValueFactory(
                new PropertyValueFactory<Model, String>("yearsWorked"));

        salaryCol = new TableColumn<Model, String>("Salary");
        salaryCol.setMinWidth(100);
        salaryCol.setCellValueFactory(
                new PropertyValueFactory<Model, String>("salary"));

        ageCol = new TableColumn<Model, String>("Age");
        ageCol.setMinWidth(100);
        ageCol.setCellValueFactory(
                new PropertyValueFactory<Model, String>("age"));




        this.getColumns().addAll(fNameCol, sNameCol, lNameCol, depCol,
                yearCol, salaryCol, ageCol);
        this.setEditable(false);

    }
}

private class WButton extends Button {
    public WButton(String desc) {
        super(desc);
        this.setPrefWidth(50);

        this.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                mList.add(new Model(addFirstName.getText(), addSecondName.getText(), addLastName.getText(),
                        addDep.getText(), addAge.getText(), addYears.getText(),
                        addSalary.getText()));



            }

        });

    }
}

private class WTree extends TreeView<String> {
    private TreeItem<String> rootItem;
    private TreeItem<String> sItem;
    private TreeItem<String> aItem;
    private TreeItem<String> iItem;
    private TreeItem<String> uItem;

    public WTree() {
        super();
        rootItem = new TreeItem<String>("Employees");
        sItem = new TreeItem<String>("Sales Department");
        aItem = new TreeItem<String>("Accounts Department");
        iItem = new TreeItem<String>("IT Support");
        uItem = new TreeItem<String>("Undercover");

        this.setRoot(rootItem);
        rootItem.getChildren().addAll(sItem, aItem, iItem, uItem);

        rootItem.setExpanded(true);
    }
}

private class WTextField extends TextField {
    public WTextField(String desc) {
        super();
        this.setPromptText(desc);
        this.setPrefWidth(100);

    }
}


private final class WMenu extends MenuBar {
    public WMenu() {
        super();
        Menu menuAdd = new Menu("Add");
        Menu menuEdit = new Menu("Edit");
        Menu addWorker = new Menu("Add Worker");
        Menu addDepartment = new Menu("Add Department");
        Menu editWorker = new Menu("Edit Worker");

        addWorker.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                  addBox.setVisible(true);
            }
        });

        this.getMenus().addAll(menuAdd, menuEdit);
        menuAdd.getItems().addAll(addWorker, addDepartment);
        menuEdit.getItems().addAll(editWorker);
    }
}

@Override
public void start(Stage primaryStage) {
    try {
        primaryStage.setTitle("Employees");
        table.setItems(mList);
        GridPane grid = new GridPane();

        addBox.getChildren().addAll(addFirstName, addSecondName, addLastName,
                addDep, addYears, addSalary, addAge, addButton);


        addBox.setVisible(false);

        grid.add(menu, 0, 0, 2, 1);
        grid.add(table, 1, 1);
        grid.add(tree, 0, 1);
        grid.add(addBox, 0, 2, 2, 1);
        Scene scene = new Scene(grid);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}
    }

称为
x
T
类型属性的正确模式为:

private ObjectProperty<T> x = new SimpleObjectProperty<>();

public ObjectProperty<T> xProperty() {
    return x ;
}
public final T getX() {
    return x.get();
}
public final void setX(T x) {
    this.x.set(x);
}
private ObjectProperty x=new SimpleObjectProperty();
公共对象属性xProperty(){
返回x;
}
公开期末考试{
返回x.get();
}
公共最终无效集x(T x){
这个.x.set(x);
}
对于基元类型和
String
s的特殊情况:使用
StringProperty
,而不是
ObjectProperty
,等等

显然,只有方法名是重要的;变量(字段或参数)可以任意调用

因此,您的
模型
类未能遵循该模式,因为:

  • 您没有任何“属性访问器”函数:
    publicstringproperty departmentProperty(){…}
  • 您的
    getDepartments()
    (原文如此)方法不是公共的
  • 您的
    getYears()
    setYears()
    方法与您在
    PropertyValueFactory
    中使用的属性名称不匹配(“年”而不是“年”)
  • 您有
    getSalary()
    但是
    setSalaray(…)

  • “属性访问器”(上文第1项)是可选的;如果省略这些属性,
    PropertyValueFactory
    将恢复使用get方法并将结果包装在
    ReadOnlyObjectWrapper
    中;请注意,这意味着如果您的表是可编辑的,则在编辑表时,您的属性不会自动更新。

    谢谢!看起来我不明白如何正确使用模式,自从我实现了你们的提示后,问题就解决了
    private ObjectProperty<T> x = new SimpleObjectProperty<>();
    
    public ObjectProperty<T> xProperty() {
        return x ;
    }
    public final T getX() {
        return x.get();
    }
    public final void setX(T x) {
        this.x.set(x);
    }