Domanda

I'm quite new to event listening and GUI's, so am having trouble figuring this out.

I have a JTabbedPane to which I have added 3 components. These components are JToolBars, which allows me to drag them out of the JTabbedPane into a floating window. This removes the tab from the tabbed pane. When I drag the JToolBar back into the pane, the tab is recreated. However, it now has an incorrect name. The name corresponds to which side of the TabbedPane the ToolBar was docked to; North, South, East or West.

Can anyone recommend a decent way of detecting that a JToolBar has been redocked and then updating the tab title? Thus far I've implemented a change listener on the tabbedPane, but cannot work out a suitable event.

Cheers.

SOLVED: The solution was to use a ContainerListener to detect a component added through the implemented componentAdded method. When a component was added to the JTabbedPane, I called a method to update the tab names with the components name, set via .setName().

public void componentAdded(ContainerEvent added) {
    updateTabs();
}

public void updateTabs() {
    for (int i = 0; i < tabbedPane.getComponents().length; i++) {
        tabbedPane.setTitleAt(i,
                tabbedPane.getComponents()[i].getName());
    }
}
È stato utile?

Soluzione

The solution was to use a ContainerListener to detect a component added through the implemented componentAdded method. When a component was added to the JTabbedPane, I called a method to update the tab names with the components name, set via .setName().

public void componentAdded(ContainerEvent added) {
    updateTabs();
}

public void updateTabs() {
    for (int i = 0; i < tabbedPane.getComponents().length; i++) {
        tabbedPane.setTitleAt(i,
                tabbedPane.getComponents()[i].getName());
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top