【发布时间】:2014-08-22 07:02:29
【问题描述】:
我正在尝试通过制作简单的应用程序来进入 JavaFX。现在我正在研究可以称为“员工数据库”的东西,这意味着将一些数据存储在表和树中,其中树按部门对员工进行排序。当您在菜单栏中点击“添加工人”标签时,hbox 正在显示,并且在归档其文本字段并单击“添加”按钮后,写入的数据应该出现在表格中。问题是 TableView 更新了除“工作年数”和“部门”列之外的所有列。我阅读了一些关于正确使用 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);
}
}
和模型类
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();
}
}
【问题讨论】:
标签: java uitableview user-interface javafx tableview