views:

241

answers:

2

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

+2  A: 

I think your call is not done on EDT. Wrap it with SwingUtilities.invokeLater

eugener
This worked - thanks!
Todd
BTW, do I need to wrap similar code that is within a ListCellRenderer? Or is that too vague a question do to where the List may be?
Todd
Any code which has to do with UI (even models) has to run on EDT. You can check if you are on it by using SwingUtilities.isEventDispatchThread()
eugener
Renderers are used in painting components, so their code runs on EDT already. But you can check to be sure
eugener
Eugene, the previously referenced code, now using invokeLater has started to exhibit the same problem as before. Any ideas?
Todd
BTW, I have wrapped it directly in the Runnable for the invokeLater, as well as used Devon's pattern below - no difference. So, it must be on the EDT.
Todd
So what is inside your Runnable now?
eugener
And is it running at all? Devon's code is not doing anything if it is on EDT
eugener
+1  A: 

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
}
Devon_C_Miller
Devon, very nice, thanks for the pattern!!
Todd