child fxml file not loading to parent fxml

2019-03-04 08:18发布

问题:

hello guys i m new to fxml so please ignore my silly question here are few things which i m try for 2 day but got not success

  1. remove white space from table i.e table size should be up to number of available row(number of row varies)
  2. when user click on table row(any display value) new fxml file is open in Anchor pane(tableview is displayed) allotted for tableview but i want to display it in main view (where whole thing table and 2 text field and search button behind this main stack i want to display here) main view (other fxml file which has only have header and sidebar)

    as show in below link of image table image of required things

here is my code

   public void initialize(URL location, ResourceBundle resources) {
    databaseHandler = DatabaseHandler.getInstance();
    initCol();

        String qu = "SELECT * FROM country_tbl ";
        ResultSet rs = databaseHandler.execQuery(qu);
            try {
                while (rs.next()) {
                 String id= rs.getString("id");
                 String name= rs.getString("countryname");
                 String description = rs.getString("descr");
                 String country2char= rs.getString("country2char");
                 String descrshort = rs.getString("descrshort");
                 data.add(new addcountryController(id,name, description,country2char,descrshort));
                }
            }catch (SQLException ex) {
                Logger.getLogger(addcountryController.class.getName()).log(Level.SEVERE, null, ex);
            }

         tableUser.setItems(null);
         tableUser.setItems(data);

         tableUser.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
         tableUser.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<addcountryController>() {
         public void onChanged(ListChangeListener.Change<? extends addcountryController> c) {
             try {

                 FXMLLoader loader =new FXMLLoader();
                 loader.load(getClass().getResource("/region/updatecountry.fxml").openStream());

                 updatecountryController usercontroller=(updatecountryController)loader.getController();
                    for (addcountryController p : c.getList()) {
                     String ADDID=p.getId();
                     String ADD=p.getName();    
                     String ADD1=p.getDescriptionshort();
                     String ADD2=p.getDescription();
                     String ADD3=p.getCountrychar();
                     usercontroller.setText(ADDID,ADD,ADD1,ADD2,ADD3);
                   }                     
                    StackPane root = loader.getRoot();
                    stackmain.getChildren().clear();
                    stackview.getChildren().add(root);     

             } catch (IOException e) {
                e.printStackTrace();
             }
          }
        });

回答1:

Hi for the first point here is the solution: You can set your cellSize in the tableView, then you will be able to use it for row getting heights(I found this the simplest way to get the rows' height). After that you have to bind the table's prefHeight to the size of your data like this:

tableView.setFixedCellSize(20);
tableView.prefHeightProperty().bind(Bindings.size(data).multiply(tableView.getFixedCellSize()).add(30));

There is an .add(30) because you have to add the tableHeaderRow's size, and this is the simplest way, to do a little experimentation and set that value explicitly. If you want the get the exact value, you can do it via .lookups to get the TableHeaderRow then get its height.

I don't really understand your second problem where do you want to display it. You can use the FXMLLoader to load that view and add to it, but if you specify a little bit better where do you exactly want to add(in which .fxml, and how is linked with the .fxml that contains the table).