lukas hansen
lukas hansen

Reputation: 83

update field when tab is selected

I'm working on a project where we use javafx, and I've come across a problem I just don't know how to solve. I'm fairly new to the whole gui thing, but this seem to be a pretty easy problem to solve, so I don't know why I can't seem to find anything useful online.

The app is composed of a lot of tabs, and we would like the tabs to call a method on the pane whenever a new tab is selected. This method would then update a combobox or talbelview depending on the specific tab.

In the class where we define the tabs and add them to a tabPane, we're able to get the selected tab by adding a listener to the tabPane, but I'm not sure if the actual Tab object got access to the functions on the pane, or anything else that might be useful.

I image the best way to solve this would be to add a listener to a "this tab/pane is selected/in-focus", but I haven't found anything like that yet.

Upvotes: 0

Views: 1384

Answers (2)

fabian
fabian

Reputation: 82461

You could add a Runnable as userData to each Tab that requires such updates and use the selection model add a listener to the selected tab that retrieves the user data and triggers the update when the tab selection changes.

Example

@Override
public void start(Stage primaryStage) {
    final Label countLabel = new Label("You visited this tab 0 time(s) before.");

    // increments visit count every time the tab is activated
    Runnable labelUpdater = new Runnable() {

        private int count = 0;

        @Override
        public void run() {
            countLabel.setText("You visited this tab " + (++count) + " time(s) before.");
        }

    };

    Tab tab1 = new Tab("tab1");
    tab1.setContent(countLabel);

    // add runnable as user data
    tab1.setUserData(labelUpdater);

    TabPane tabPane = new TabPane(tab1, new Tab("tab2"));

    // execute update when a newly selected tab contains a updater 
    tabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue != null) {
            Runnable updater = (Runnable) newValue.getUserData();
            if (updater != null) {
                updater.run();
            }
        }
    });

    Scene scene = new Scene(tabPane, 300, 300);

    primaryStage.setScene(scene);
    primaryStage.show();
}

You could also set the updater as user data to the Tab's content, if this is more convenient for you:

//    tab1.setUserData(labelUpdater);
    countLabel.setUserData(labelUpdater);

    TabPane tabPane = new TabPane(tab1, new Tab("tab2"));
    tabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue != null) {
            Node content = newValue.getContent();
            if (content != null) {
                Runnable updater = (Runnable) content.getUserData();
                if (updater != null) {
                    updater.run();
                }
            }
        }
    });

Upvotes: 2

If you look in here you can under field summary the event you are looking for ie. SELECTION_CHANGED_EVENT. Just add a listener for this event for each of your tabs and you should be golden.

Upvotes: 0

Related Questions