简体   繁体   中英

How does one call a method on the changing of a Tab in JavaFx?

Ok so here is what i am trying to do: I have a page called 'SettingsPage'. Now, in this page i Have a tabPane with 3 tabs; users, buttons, and sales. Buttons and Users both have tables which need to be populated with data before they are viewed by the user. When I was first making the program I just used a 'initialize' method to populate the user table on the opening of 'SettingsPage'. However, now that I am beginning to try and populate the buttons table, I am encountering a memory error because now there is too much data to be loaded at once together.

So, I though a good solution to this would be to make an Event method that is called whenever a certain tab is opened. I am using SceneBuilder atm, and what seemed to be the equivalent of this was onSelectionChanged, however I can't seem to use this as you would use 'methodEg(ActionEvent event)...'. So my question is, how can ensure that a method will be called on the opening of a certain tab.

For example, when 'buttonTab' is clicked, the 'populateButtonTable' method is called.

The answer to this was this code:

And if anyone asks why I didn't use.isSelected()... I did, it for some reason didn't work, but what I have done below does, so I dunno...

'''

@FXML public void initialize(URL url, ResourceBundle rb){


   tabs.getSelectionModel().selectedItemProperty().addListener(
            new ChangeListener<Tab>() {
                @Override
                public void changed(ObservableValue<? extends Tab> ov, Tab t, Tab t1) 
                {                        
                    if (tabs.getSelectionModel().getSelectedItem() == ExampleTab)
                    {
                        //do whatever
                    }      
                }
             }
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM