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:
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:
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!