Bringing tab to front in JTabbedPane

妖精的绣舞 提交于 2019-12-24 06:24:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!