我正在尝试创建一个使用tableview的物料库存程序。我有一个控制器,它将根据该tableview显示统计信息,但我不确定如何从该视图访问实时数据。
我的MainApp加载最后保存的表,而我的MaterialOverviewController具有tableview。
我有一个RootLayoutController,它是我的主布局控制器,它有一个简单的文件菜单,但也是加载OnFloorStatsController的地方。
我可以在加载OnFloorStatsController时显然创建一个新的MainApp,但这只会加载默认输入的样本数据。
如何从MainApp访问实时数据?
这是我的MainApp代码:
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private ObservableList<Material> materialData = FXCollections.observableArrayList();
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Label Inventory");
this.primaryStage.getIcons().add(new Image("file:resources/images/1462487405_Mask.png"));
initRootLayout();
showMaterialOverview();
}
public MainApp() {
// Add some sample data
}
public ObservableList<Material> getMaterialData() {
return materialData;
}
/**
* Initializes the root layout and tries to load the last opened
* material file.
*/
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class
.getResource("RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
// Give the controller access to the main app.
RootLayoutController controller = loader.getController();
controller.setMainApp(this);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
// Try to load last opened person file.
File file = getMaterialFilePath();
if (file != null) {
loadMaterialDataFromFile(file);
}
}
public void showMaterialOverview() {
try {
// Load material overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("MaterialOverview.fxml"));
AnchorPane materialOverview = (AnchorPane) loader.load();
// Set material overview into the center of root layout.
rootLayout.setCenter(materialOverview);
// Give the controller access to the main app.
MaterialOverviewController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public void showOnFloorStatistics() {
try {
// Load the fxml file and create a new stage for the popup.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("OnFloorStats.fxml"));
AnchorPane page = (AnchorPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Material On Floor");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Set the material into the controller.
OnFloorStatsController controller = loader.getController();
controller.start(dialogStage);
dialogStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showBarChartTest() {
try {
// Load the fxml file and create a new stage for the popup.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("BarChartTestLayout.fxml"));
AnchorPane page = (AnchorPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Detailed Bar Chart");
//
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Set the material into the controller.
BarChartTest controller = loader.getController();
controller.start(dialogStage);
dialogStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
* @return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
这是我的MaterialOverViewController代码:
public class MaterialOverviewController {
@FXML
private TableView<Material> materialTable;
private List<Material> materialId;
public TableView<Material> getMaterialTable() {
return materialTable;
}
// public void setMaterialTable(TableView<Material> materialTable) {
// this.materialTable = materialTable;
}
private ObservableList<Integer> rollsTotal = FXCollections.observableArrayList();
private ObservableList<String> materialNames = FXCollections.observableArrayList();
// Reference to the main application.
private MainApp mainApp;
/**
* The constructor.
* The constructor is called before the initialize() method.
*/
public MaterialOverviewController() {
}
/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded.
*/
@FXML
private void initialize() {
materialTotal = 0;
// Initialize the material table with columns.
pONumberColumn.setCellValueFactory(cellData -> cellData.getValue().pONumberProperty());
materialNameColumn.setCellValueFactory(cellData -> cellData.getValue().materialNameProperty());
materialIdColumn.setCellValueFactory(cellData -> cellData.getValue().materialIdProperty());
materialWidthColumn.setCellValueFactory(cellData -> cellData.getValue().materialWidthProperty().asObject());
qtyOrderedColumn.setCellValueFactory(cellData -> cellData.getValue().qtyOrderedProperty().asObject());
qtyReceivedColumn.setCellValueFactory(cellData -> cellData.getValue().qtyReceivedProperty().asObject());
qtyBackOrderedColumn.setCellValueFactory(cellData -> cellData.getValue().qtyBackOrderedProperty().asObject());
// qtyBackOrderedColumn.setCellValueFactory(cellData -> cellData.getValue().qtyBackOrderedProperty().asObject());
qtyOnFloorColumn.setCellValueFactory(cellData -> cellData.getValue().qtyOnFloorProperty().asObject());
// dateOrderedColumn.setCellValueFactory(cellData -> cellData.getValue().dateOrderedProperty().asString());
dateOrderedColumn.setCellValueFactory(cellData -> cellData.getValue().dateOrderedProperty().asString());
estArrivalColumn.setCellValueFactory(cellData -> cellData.getValue().dateExpectedProperty().asString());
supplierColumn.setCellValueFactory(cellData -> cellData.getValue().supplierProperty());
// Clear material details.
showMaterialDetails(null);
// Listen for selection changes and show the material details when changed.
materialTable.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> showMaterialDetails(newValue));
}
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
// Add observable list data to the table
materialTable.setItems(mainApp.getMaterialData());
}
private void showMaterialDetails(Material material) {
if (material != null) {
// Fill the labels with info from the material object.
pONumberLabel.setText(material.getPoNumber());
materialIdLabel.setText(material.getMaterialId());
materialNameLabel.setText(material.getMaterialName());
materialWidthLabel.setText(String.valueOf(material.getMaterialWidth()));
qtyOrderedLabel.setText(Integer.toString(material.getQtyOrdered()));
qtyRecievedLabel.setText(Integer.toString(material.getQtyReceived()));
qtyBackOrderedLabel.setText(Integer.toString(material.getQtyOrdered()-material.getQtyReceived()));
// qtyBackOrderedLabel.setText(Integer.toString(material.getQtyBackOrdered()));
qtyOnFloorLabel.setText(Integer.toString(material.getQtyOnFloor()));
// dateOrderedLabel.setText(DateUtil.format(material.getDateOrdered()));
// dateOrderedLabel.equals(DateUtil.format(material.getDateOrdered()));
// dateOrderedLabel.setText(DatePicker(String.valueOf(material.getDateOrdered());
// estArrivalLabel.setText(material.getSupplier());
supplierLabel.setText(material.getSupplier());
String pattern = "MM/dd/yyyy";
// dateTestTextField.setText(DateUtil.format(material.getBirthday()));
dateOrderedLabel.setText(DateUtil.format(material.getDateOrdered()));
estArrivalLabel.setText(DateUtil.format(material.getDateExpected()));
//TODO: set running material totals
// getMaterialNames(Arrays.asList(material));
handleSelectMaterial();
}
}
}
public List getMaterialNames(List<Material> materials) {
for (Material name : materials) {
mainApp.getMaterialData();
if (!materialNames.contains(name)) {
materialNames.add(name.getMaterialName());
}
}
for (String name : materialNames) {
materialTextField.setText(name + "\n");
}
return Arrays.asList(materialNames);
}
}
}
以下是我的RootLayoutController的一些代码,但我不知道为什么我不能从OnFloorStatsController做同样的事情:
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
@FXML
private void handlePrint() {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("MaterialOverview.fxml"));
try {
AnchorPane frame = fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
MaterialOverviewController c = fxmlLoader.getController();
//Loads data into table
c.setMainApp(mainApp);
我只需要访问MaterialOvewViewController
中的实时数据这是OnFloorStatsController:
private MainApp mainApp;
public TableView<Material> material;
/**
* Is called by the main application to give a reference back to itself.
*
* @param mainApp
*/
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
// final static String austria = "14532";
// final static String brazil = "78333";
// final static String france = "40443";
// final static String italy = "72825";
// final static String usa = "72829";
public void start(Stage stage) {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("MaterialOverview.fxml"));
try {
AnchorPane frame = fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
MaterialOverviewController c = fxmlLoader.getController();
//Loads data into table
c.setMainApp(mainApp);
stage.setTitle("Material On Floor");
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
final BarChart<String, Number> bc =
new BarChart<String, Number>(xAxis, yAxis);
bc.setTitle("Material On Floor");
xAxis.setLabel("Material");
yAxis.setLabel("Quantity");
XYChart.Series series1 = new XYChart.Series();
series1.setName("6 inch");
Integer floorTotal = 0;
for (Material id :mainApp.getMaterialData()) {
if (id.getMaterialWidth() < 6.9) {
floorTotal = id.getQtyOnFloor();
for (Material size : mainApp.getMaterialData()) {
if ( id.getMaterialId().equalsIgnoreCase(size.getMaterialId()) &&
size.getMaterialWidth() == id.getMaterialWidth()) {
floorTotal += size.getQtyOnFloor();
}
else {
floorTotal = id.getQtyOnFloor();
}