Fire stateChanged event on JTabbedPane

后端 未结 2 971
后悔当初
后悔当初 2021-01-21 12:53

I have JTabbedPane with fade animation, which is performed when user clicks tabs. To handle animation I override stateChanged method.

public class A         


        
2条回答
  •  青春惊慌失措
    2021-01-21 13:30

    I found the real reason of my problem.

    It was the thread problem, not the animation itself. I was calling setSelectedIndex outside EDT so my JTabbedPane was updated instantly and then the animation from EDT was performed.

    The anserw is:

    public void setSelectedTab(final int tab) throws InterruptedException, InvocationTargetException{
        if(!SwingUtilities.isEventDispatchThread()){
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    animatedTabbedPane.setSelectedIndex(tab);
                }
            });
        }
        else
            animatedTabbedPane.setSelectedIndex(tab);
    }
    

    Changing tabs inside EDT doesn't couse unwanted flash anymore.

提交回复
热议问题