1

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!

Community
  • 1
  • 1
dubskiski
  • 67
  • 2
  • 12

2 Answers2

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.

dubskiski
  • 67
  • 2
  • 12
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

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.

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.

  1. The Controller class for the F2.

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.

bleik urrego
  • 90
  • 10