JTabbedPane, how to determine if a user has left a tab?

Interestingly, if it could be determined that the user left a specific tab. For example, we have 2 tabs: "omg" and "lol". The current tab is omg. I want to know that the user has switched from "omg" to "lol"

+3
source share
2 answers

By adding a change listener to JTabbedPane, you will know when the tab changes.

Update: Added tab index tracking

tabbedPane.getModel().addChangeListener(new ChangeListener() {
    int lastTabIndex = -1;
    public void stateChanged(ChangeEvent e) {
         int newIndex = tabbedPane.getSelectedIndex();
         if (lastTabIndex == 1 && newIndex == 2) { //or whatever check/combination of checks you would like
             //switched from tab 1 to tab 2!
         }

         //or just check for leaving tab 1
         if (lastTabIndex == 1) {
             //left tab 1!
         }

         //etc

         lastTabIndex = newIndex;
    }
});
+4
source

, , , JTabbedPane setSelectedIndex (int). , GOING , , . super.setSelectedIndex(int), ( , ).

+2

All Articles