I am creating an inventory management system and am trying to query a database from a selected tableView row and populate a new tab with the information retrieved from the database and loaded with a new fxml and controller. I have found the answer to create a new stage and scene, which is working fine now from: Passing Parameters JavaFX FXML But I have no idea how to get it to show inside a tab of an fxml file that I already have open. Below is my code, there is a bit of info commented out from when I was trying to get the data to load in the tab (I realized I was creating a new instance of an fxml controller when I loaded it so it was returning null pointer exceptions because although I had the data it wasn't loaded into the new instance of the controller). My code is:
我正在创建一个库存管理系统,并尝试从选定的tableView行查询数据库,并使用从数据库中检索到的信息填充新选项卡,并使用新的fxml和控制器加载。我找到了创建一个新阶段和场景的答案,现在工作正常:传递参数JavaFX FXML但是我不知道如何让它显示在我已经打开的fxml文件的选项卡中。下面是我的代码,当我试图在选项卡中加载数据时,有一些信息被注释掉(我意识到我在加载它时创建了一个fxml控制器的新实例,所以它返回空指针例外,因为虽然我有数据但它没有加载到控制器的新实例中。我的代码是:
state = departmentCB.getValue();
if (state.equals(aseptic)) {
ResultSet rs = null;
PreparedStatement selectedItemStmt = null;
Connection con = null;
try {
con = dBC.getDBConnection();
String selectedPart = tableView.getSelectionModel().getSelectedItem().getModel_number();
String sql = "select * from aseptic_parts_list where model_number = ?";
selectedItemStmt = con.prepareStatement(sql); // create a statement
selectedItemStmt.setString(1, selectedPart); // set input parameter
rs = selectedItemStmt.executeQuery();
System.out.println(rs);
// extract data from the ResultSet
while (rs.next()) {
id = rs.getInt(1);
manufacturer_name = rs.getString(2);
model_number = rs.getString(3);
vendor_name = rs.getString(4);
vendor_part_number = rs.getString(5);
tolmar_part_number = rs.getString(6);
part_location = rs.getString(7);
price = rs.getDouble(8);
quantity = rs.getInt(9);
min = rs.getInt(10);
max = rs.getInt(11);
img = rs.getString(12);
equipment_group = rs.getString(13);
equipment_id = rs.getString(14);
additional_notes = rs.getString(15);
description = rs.getString(16);
ItemController ic = new ItemController();
///ic.setTextItems(id, manufacturer_name, model_number, vendor_name, vendor_part_number, tolmar_part_number, part_location, price, quantity,
// min, max, img, equipment_group, equipment_id, additional_notes, description);
//ic.descriptionLbl.setText(description);
Tab tab = new Tab();
tabs.getTabs().add(tab);
tab.setText(tableView.getSelectionModel().getSelectedItem().getDescription());
FXMLLoader loader = new FXMLLoader(
getClass().getResource(
"Item.fxml"
)
);
Stage stage = new Stage(StageStyle.DECORATED);
stage.setScene(
new Scene(
(Pane) loader.load()
)
);
ItemController controller =
loader.<ItemController>getController();
controller.setTextItems(id, manufacturer_name, model_number, vendor_name, vendor_part_number, tolmar_part_number, part_location, price, quantity,
min, max, img, equipment_group, equipment_id, additional_notes, description);
stage.show();
//tab.setContent((Node) FXMLLoader.load(this.getClass().getResource("Item.fxml")));
//loader.setController(ic);
//ic.descriptionLbl.setText(ic.getDescription());
ic.descriptionLbl.setText(description);
System.out.println(description + "this works");
System.out.println(id + "\t" + manufacturer_name + "\t" + model_number + "\t" + description+ "\t" + equipment_id + "\t" + part_location);
}
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
finally {
try {
rs.close();
selectedItemStmt.close();
con.close();
} catch (SQLException e) {
System.out.println(e);
e.printStackTrace();
}
}
} else if (state.equals(general)) {
} else if (state.equals(facilities)) {
}
}
}
});
Please help with how to get this data into a new tab versus opening a new stage, below is the info from the ItemController, it is just updating all the labels with the particular parts information retrieved from the database:
请帮助了解如何将这些数据放入新选项卡而不是打开一个新阶段,下面是ItemController的信息,它只是使用从数据库中检索到的特定部件信息更新所有标签:
public void setTextItems (int id, String manufacturer_name, String model_number, String vendor_name, String vendor_part_number, String tolmar_part_number,
String part_location, double price, int quantity, int min, int max, String img, String equipment_group, String equipment_id,
String additional_notes, String description) {
descriptionLbl.setText(description);
vendorPartNumberLbl.setText(vendor_part_number);
}
This is not complete yet, but you get the idea, thanks!
这还没有完成,但你明白了,谢谢!
2 个解决方案
#1
0
I found that:
我找到:
tab.setContent((Node) loader.load());
I had tried this before just passing through loader, but it needs the load() method attached and it is now loading inside a tab in the main fxml document.
我在通过loader之前尝试过这个,但它需要附加load()方法,现在它正在主fxml文档的一个标签内加载。
#2
0
I'm not sure if this is the proper way of doing what you are asking, but pragmatically speaking. You'll need to create a couple of classes to organize what you are doing:
我不确定这是否是你正在做的事情的正确方式,但实际上是这样。您需要创建几个类来组织您正在做的事情:
1.The Main class should hold your FXML (F1) loader for the Main Stage and set up Stage
1.主类应该为主舞台保存你的FXML(F1)加载器并设置舞台
2.The Controller class of this FXML (F1) should create the TabPane and Tabs. This class should also have a HashMap to track the controllers for the Tabs.
2.此FXML(F1)的Controller类应创建TabPane和Tabs。该类还应该有一个HashMap来跟踪Tabs的控制器。
3.The 3rd Class should hold the FXML (F2) loader for the content in your Pane thats going into the Tab and it should also set the F2 controller as an object. As well as a getPane() function to get the Content.
3.第3类应该为您的Pane内容中的内容保存FXML(F2)加载器,它还应该将F2控制器设置为对象。以及获取内容的getPane()函数。
- The Controller class for the F2.
- F2的Controller类。
Once you have this set up you can call your tab content and controller by:
完成此设置后,您可以通过以下方式调用选项卡内容和控制器:
public void addTab(ActionEvent event){
Tab newTab = new Tab();
F2 f2 = new F2(); // new FXML loader Object
newTab.setContent(f2.getContent());
ControllerHash.put('key', f2.getController); // HashMap - binding the content of new Tab with controller
}
One of the classes should be the loader for the FXML file and controller.
其中一个类应该是FXML文件和控制器的加载器。
#1
0
I found that:
我找到:
tab.setContent((Node) loader.load());
I had tried this before just passing through loader, but it needs the load() method attached and it is now loading inside a tab in the main fxml document.
我在通过loader之前尝试过这个,但它需要附加load()方法,现在它正在主fxml文档的一个标签内加载。
#2
0
I'm not sure if this is the proper way of doing what you are asking, but pragmatically speaking. You'll need to create a couple of classes to organize what you are doing:
我不确定这是否是你正在做的事情的正确方式,但实际上是这样。您需要创建几个类来组织您正在做的事情:
1.The Main class should hold your FXML (F1) loader for the Main Stage and set up Stage
1.主类应该为主舞台保存你的FXML(F1)加载器并设置舞台
2.The Controller class of this FXML (F1) should create the TabPane and Tabs. This class should also have a HashMap to track the controllers for the Tabs.
2.此FXML(F1)的Controller类应创建TabPane和Tabs。该类还应该有一个HashMap来跟踪Tabs的控制器。
3.The 3rd Class should hold the FXML (F2) loader for the content in your Pane thats going into the Tab and it should also set the F2 controller as an object. As well as a getPane() function to get the Content.
3.第3类应该为您的Pane内容中的内容保存FXML(F2)加载器,它还应该将F2控制器设置为对象。以及获取内容的getPane()函数。
- The Controller class for the F2.
- F2的Controller类。
Once you have this set up you can call your tab content and controller by:
完成此设置后,您可以通过以下方式调用选项卡内容和控制器:
public void addTab(ActionEvent event){
Tab newTab = new Tab();
F2 f2 = new F2(); // new FXML loader Object
newTab.setContent(f2.getContent());
ControllerHash.put('key', f2.getController); // HashMap - binding the content of new Tab with controller
}
One of the classes should be the loader for the FXML file and controller.
其中一个类应该是FXML文件和控制器的加载器。