Fire stateChanged event on JTabbedPane

后端 未结 2 944
后悔当初
后悔当初 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:42

    One approach would be to add an AncestorListener to each tab's content. Let the listener trigger the desired effect as the tab is added to or removed from visibility.

    image

    import java.awt.EventQueue;
    import java.util.Date;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.event.AncestorEvent;
    import javax.swing.event.AncestorListener;
    
    /**
     * @see http://stackoverflow.com/a/17993449/230513
     */
    public class Test {
    
        private void display() {
            JFrame f = new JFrame("Test");
            final JTabbedPane jtp = new JTabbedPane();
            jtp.add("One", createPanel());
            jtp.add("Two", createPanel());
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(jtp);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        private JPanel createPanel() {
            JPanel panel = new JPanel();
            final JLabel label = new JLabel(new Date().toString());
            panel.add(label);
            panel.addAncestorListener(new AncestorListener() {
                @Override
                public void ancestorAdded(AncestorEvent event) {
                    // start animation
                    label.setText(new Date().toString());
                }
    
                @Override
                public void ancestorRemoved(AncestorEvent event) {
                    // stop animation
                }
    
                @Override
                public void ancestorMoved(AncestorEvent event) {
                }
            });
            return panel;
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Test().display();
                }
            });
        }
    }
    

提交回复
热议问题