4

Let's say I have JTabbedPane with a ChangeListener

JTabbedPane tabbedPane = new JTabbedPane();

// Add few tabs
.....
.....

tabbedPane.addChangeListener(new ChangeListener() {
  public void stateChanged(ChangeEvent changeEvent) {
    // How to determine if the changeEvent was fired because of a tab remove/add ?
  }
});

and somewhere I am doing a

tabbedPane.removeTabAt(2);

and somewhere else

tabbedPane.add(panel, 0);

The ChangeListener should get fired now, is there any way to determine within the listener if it was called due to a tab remove/add ?

EDIT: I am basically trying to do some actions only when the user switches between tabs and not when adding or removing.

Krishnaraj
  • 2,360
  • 1
  • 32
  • 55
  • 2
    Can you elaborate on how the action should respond differently. – trashgod Aug 20 '12 at 11:47
  • There is a combobox outside the tabbedPane which needs to be modified only when the user switches between tabs and a change in the combobox can in turn add/remove tabs! – Krishnaraj Aug 20 '12 at 11:57
  • hmm ... so it _is_ related to the tab component, that is the combo would need to be updated when the selected tab is removed? – kleopatra Aug 20 '12 at 12:00
  • yes but the combo need not be updated when the tab is added back – Krishnaraj Aug 20 '12 at 12:09
  • adding it back doesn't change the selected _component_ - as long as you have no custom logic which auto-selects the added - so my solution should work fine. If not, please add an SSCCE to demonstrate your exact context – kleopatra Aug 20 '12 at 12:56

4 Answers4

7

If I remember correctly, JTabbedPane will fire a componentAdded() event (defined in Container) when a new tab is added and a componentRemoved() event if a tab is removed.

You should be able to listen for adding or removal of a tab by registering a ContainerListener

http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#addContainerListener(java.awt.event.ContainerListener)

The stateChanged() event is just a side-effect of the add because the JTabbedPanel automatically switches to the new tab.

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
2

You might also want to examine the client property __index_to_remove__, which is set by removeTabAt().

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 2
    hmm ... doesn't help when adding a tab, afaics? – kleopatra Aug 20 '12 at 11:33
  • @kleopatra: Good point; I also missed the update to the question. Adding a similar property to the `addTabAt()` methods seems awkward. Requesting clarification above. – trashgod Aug 20 '12 at 11:48
2

Depending on the exact requirement, you might keep track of the selected component and only do stuff if that has changed:

ChangeListener l = new ChangeListener() {

    Component lastSelected = tabbedPane.getSelectedComponent();
    @Override
    public void stateChanged(ChangeEvent e) {
        if (lastSelected != tabbedPane.getSelectedComponent()) {
            LOG.info("changed: " + tabbedPane.getSelectedIndex());
            lastSelected = tabbedPane.getSelectedComponent();
        }

    }
};
tabbedPane.addChangeListener(l);

Might not be good enough, though, as it will trigger if the selected tab itself is removed.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • +1 I don't see an easier way. I notice that `fireStateChanged()` occurs `if (index == selected)`. – trashgod Aug 20 '12 at 11:56
  • having a flag to keep track of all add/remove operations seems to be the only reliable way :(. Really surprised that ChangeEvent doesn't have any state information. This should be a common problem, ain't it? – Krishnaraj Aug 21 '12 at 06:12
  • accepting this answer as this could be good enough for many ppl – Krishnaraj Aug 21 '12 at 06:12
0

By keeping track of the current number of tabs, you can detect a selection change based on add or delete

ChangeListener l = new ChangeListener() {

    int lastTabCount = tabbedPane.getTabCount();
    Component lastSelected = tabbedPane.getSelectedComponent();

    @Override
    public void stateChanged(ChangeEvent e) {
        if (lastSelected != tabbedPane.getSelectedIndex())
        {
            int currentTabCount = tabbedPane.getTabCount();
            if (lastTabCount == currentTabCount ) {
                LOG.info("changed: " + tabbedPane.getSelectedIndex());
            } else if (lastTabCount < currentTabCount)
                LOG.info("changed due to delete: " + tabbedPane.getSelectedIndex());
            } else if (lastTabCount > currentTabCount)
                LOG.info("changed due to add: " + tabbedPane.getSelectedIndex());
            }
            lastTabCount = tabbedPane.getTabCount();
            lastSelected = tabbedPane.getSelectedComponent();
        }

    }
};
tabbedPane.addChangeListener(l);
Gno Whey
  • 1
  • 1
  • 1