问题
When I use setSelectedComponent
or setSelectedIndex
on a JTabbedPane
object, the panel always comes up in my UI. However, sometimes the tab associated with the panel remains hidden. In other words, the tab does not scroll to a visible portion of the tabbed pane.
How can I fix this? I have tried the cheesy select one index, then select desired index, as well as several other more elegant things, but arrrrgh!!
Help me if you can.
Thanks, Todd
回答1:
I think your call is not done on EDT. Wrap it with SwingUtilities.invokeLater
回答2:
Here is a patter you can use if you have a method that alters swing components, or their models and so must be called on the EDT, but may be called from a background thread. This ensures func always runs on the EDT:
void func(final Type1 arg1, final Type2 arg2) {
if (!SwingUtilities.isEventDispatchThread()) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
func(arg1, arg2);
}
});
return;
}
// method code goes here
}
来源:https://stackoverflow.com/questions/1235644/bringing-tab-to-front-in-jtabbedpane