Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top