我一直在绝望地试图想出这个,但我没有运气。我的想法是,当我输入信息并按“保存员工”按钮时,中心的表格应相应更新。我知道达到这个的方法让我相信我创建表和列表的方式有问题。
编辑过的miminal代码:
控制器:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
public class Controller implements Initializable {
@FXML // fx:id="nameField"
private TextField nameField;
@FXML // fx:id="wageField"
private TextField wageField;
@FXML // fx:id="saveBtn"
private Button saveBtn;
@FXML // fx:id="nameColumn"
private TableColumn<Employee, String> nameColumn;
@FXML // fx:id="wageColumn"
private TableColumn<Employee, Double> wageColumn;
private TableView<Employee> employeeTable;
ObservableList<Employee> employeeList = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources) {
nameColumn.setCellValueFactory(dataCell -> dataCell.getValue().nameProperty());
wageColumn.setCellValueFactory(dataCell -> dataCell.getValue().wageProperty().asObject());
}
public void saveButtonClick() {
Employee employee = new Employee();
employee.setName(nameField.getText());
employee.setWage(Double.parseDouble(wageField.getText()));
employeeList.add(employee);
}
}
员工:
package application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Employee {
private StringProperty name = new SimpleStringProperty(this, "name", "");
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
private DoubleProperty wage = new SimpleDoubleProperty(this, "wage", 0.0);
public Double getWage() {
return wage.get();
}
public DoubleProperty wageProperty() {
return wage;
}
public void setWage(Double wage) {
this.wage.set(wage);
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<center>
<TableView prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="nameColumn" prefWidth="75.0" text="Name" />
<TableColumn fx:id="wageColumn" prefWidth="75.0" text="Wage" />
</columns>
</TableView>
</center>
<left>
<VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<Label text="Add new Entry" />
<TextField fx:id="nameField" promptText="name" />
<TextField fx:id="wageField" promptText="wage" />
<Button fx:id="saveBtn" mnemonicParsing="false" onAction="#saveButtonClick" text="save new entry" />
</children>
</VBox>
</left>
</BorderPane>
主:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("debug.fxml"));
primaryStage.setTitle("Minimal Debug");
primaryStage.setScene(new Scene(root, 1920, 1080));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
由于
答案 0 :(得分:3)
您需要将TableView
注入控制器,就像使用其他控件一样。即。
// add @FXML annotation
@FXML
private TableView<Employee> employeeTable;
和
<!-- note fx:id attribute -->
<TableView fx:id="employeeTable" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
然后您可以使用initialize()
方法将数据设置为表格:
@Override
public void initialize(URL location, ResourceBundle resources) {
nameColumn.setCellValueFactory(dataCell -> dataCell.getValue().nameProperty());
wageColumn.setCellValueFactory(dataCell -> dataCell.getValue().wageProperty().asObject());
employeeTable.setItems(employeeList);
}