TableView不会更新所有列

时间:2014-07-01 16:16:47

标签: java uitableview user-interface javafx tableview

我试图通过制作简单的应用程序来进入JavaFX。现在我正在做一些可以被称为"员工数据库"这意味着将一些数据存储在表和树中,其中树按部门对员工进行排序。当你点击"添加工人" MenuBar hbox中的标签正在显示,并在归档其文本字段并单击&#34之后添加"按钮写入的数据应该出现在表格中。问题是TableView会更新所有列,除了" Years Worked"和"部门"柱。我阅读了一些关于使用cannonical变量名称的提示,以获得正确的值,但我仍然无法克服这个问题。

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);
}
    }

和Model类

    public class Model {

private final SimpleStringProperty firstNameProperty;
private final SimpleStringProperty secondNameProperty;
private final SimpleStringProperty lastNameProperty;
private final SimpleStringProperty departmentProperty;
private final SimpleStringProperty ageProperty;
private final SimpleStringProperty yearsWorkedProperty;
private final SimpleStringProperty salaryProperty;

public Model(String fName, String sName, String lName, 
        String func, String age, String years, String salary) {
    this.firstNameProperty = new SimpleStringProperty(fName);
    this.lastNameProperty = new SimpleStringProperty(lName);
    this.secondNameProperty = new SimpleStringProperty(sName);
    this.departmentProperty = new SimpleStringProperty(func);
    this.ageProperty = new SimpleStringProperty(age);
    this.yearsWorkedProperty = new SimpleStringProperty(years);
    this.salaryProperty = new SimpleStringProperty(salary);
}

public void setFirstName(String newName) {
    firstNameProperty.set(newName);
}

public String getFirstName() {

    return firstNameProperty.get();

}

public void setLastName(String newName) {
    lastNameProperty.set(newName);
}

public String getLastName() {

    return lastNameProperty.get();

}

public void setSecondName(String newName) {
    secondNameProperty.set(newName);
}

public String getSecondName() {

    return secondNameProperty.get();
}

public void setAge(String newAge) {
    ageProperty.set(newAge);
}

public String getAge() {
    return ageProperty.get();
}

public void setYears(String newY) {
    yearsWorkedProperty.set(newY);
}

public String getYears() {
    return yearsWorkedProperty.get();
}

public void setSalaray(String newSal) {
    salaryProperty.set(newSal);
}

public String getSalary() {
    return salaryProperty.get();
}

public void setDepartment(String newFunc) {
    departmentProperty.set(newFunc);
}

String getDepartment() {
    return departmentProperty.get();
}
    }

1 个答案:

答案 0 :(得分:0)

名为T的{​​{1}}类型属性的正确模式为:

x

包含基本类型和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); } s的特殊情况:代替String,使用ObjectProperty<String>等。

显然只有方法名称很重要;变量(字段或参数)可以随意调用。

所以你的StringProperty类无法遵循这种模式,因为:

  1. 您没有任何“属性访问者”功能:Model等。
  2. 您的public StringProperty departmentProperty() {...} sic )方法不公开
  3. 您的getDepartament()getYears()方法与您在setYears()中使用的属性名称不匹配(“年”代替“年工作”)
  4. 您有PropertyValueFactorygetSalary()
  5. “属性访问者”(上面的第1项)在某种程度上是可选的;如果省略这些,setSalaray(...)将恢复使用get方法并将结果包装在PropertyValueFactory;请注意,这意味着如果您的表格是可编辑的,则在编辑表格时不会自动更新您的属性。