Swing AncestorListener:不要开关切换标签?

时间:2013-03-21 15:54:11

标签: java swing jtabbedpane ancestor

我正在使用以下代码让我的组件分别在添加/删除时注册/取消注册事件侦听器。

addAncestorListener(new AncestorListener() {
        @Override public void ancestorRemoved(AncestorEvent event) {
            log.info("========= Remove listeners by " + ContentViewingComponent.this);
            ...
        }
        @Override public void ancestorMoved(AncestorEvent event) {}
        @Override public void ancestorAdded(AncestorEvent event) {
            log.info("========= Add listeners by " + ContentViewingComponent.this);
            ...
        }
    });

我的意图是,在大多数情况下,即在创建组件时,两个事件(添加和删除)基本上都会触发一次。添加,当它被删除&遗忘。

然而,事实证明,当选择/隐藏此组件所在的选项卡(JTabbedPane)时,它们也会被触发。有没有办法绕过/发现它?

可能使用显示/隐藏而非添加/删除的JTabbedPane版本?更新:更多调查显示该组件实际上未被删除,但隐藏了标签,{ {1}}也为此而激怒。我可以检测组件是否仍然具有root *并忽略该事件(如果有),但问题是当实际删除组件时(不可见),它不会再次触发。

* 嗯,还有待观察。在实际删除之前看起来ancestorRemoved()会触发,因此我无法走向ancestorRemoved()链以查明该组件是否具有根。更新:通过使用{后一步检查它来修复{1}}。


(对于那些熟悉Flash / AS3的人,我正在寻找与getParent() / SwingUtilities.invokeLater()事件相当的Swing。)

2 个答案:

答案 0 :(得分:3)

使用HierarchyListener修复。

public static void addDisplayableListeners(final Component comp, final Runnable onDisplayable, final Runnable onNotDisplayable) {
    comp.addHierarchyListener(new HierarchyListener() {
        @Override public void hierarchyChanged(HierarchyEvent e) {
            System.out.println("HIERARCHY CHANGE ===== " + comp);
            final boolean dc = (e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0;
            if (dc) {
                System.out.println("DISPLAYABILITY_CHANGED");
                if (comp.isDisplayable()) {
                    onDisplayable.run();
                } else {
                    onNotDisplayable.run();
                }
            }
        }
    });
}

旧版本:

    addHierarchyListener(new HierarchyListener() {

        private boolean hadRoot = false;

        @Override public void hierarchyChanged(HierarchyEvent e) {

            System.out.println("HIERARCHY CHANGE ===== " + ContentViewingComponent.this);

            final boolean hasRoot = Swing.hasRoot(ContentViewingComponent.this);
            System.out.println("Has root: " + hasRoot);

            if (hasRoot != hadRoot) {

                System.out.println("...which is not what was.");
                hadRoot = hasRoot;

                if (hasRoot) {
                    log.info("========= Add listeners by " + ContentViewingComponent.this);
                    ...
                } else {
                    log.info("========= Remove listeners by " + ContentViewingComponent.this);
                    ...
                }
            }

        }
    });

注意:我会看看我是否可以使用@ mKorbel答案中提到的官方方法替换我自己的助手(hadRoothasRoot())。

答案 1 :(得分:2)

  

我的意图是两个事件(添加和删除)基本上都会触发   一旦在大多数情况下,即在创建组件时添加   当它被删除&遗忘

有AncestorListener,ComponentListener和HierarchyListener,并且可以是异步的,通过使用SwingUtilities从这些事件或祖先中获取getComponent的方式相同

例如

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class MyTabbedPane {

    private JTabbedPane tabbedPane = new JTabbedPane();
    private JFrame f = new JFrame();
    private JLabel label1 = new JLabel("Tab1");
    private JLabel label2 = new JLabel("Tab2");
    private JLabel label3 = new JLabel("Tab3");
    private JLabel label4 = new JLabel("Tab4");

    public MyTabbedPane() {
        tabbedPane.addTab("Tab1", label1);
        tabbedPane.addTab("Tab2", label2);
        tabbedPane.addTab("Tab3", label3);
        tabbedPane.addTab("Tab4", label4);
        tabbedPane.setTabPlacement(JTabbedPane.TOP);
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        label1.addAncestorListener(new EventHandler());
        label2.addAncestorListener(new EventHandler());
        label3.addAncestorListener(new EventHandler());
        label4.addAncestorListener(new EventHandler());

        label1.addHierarchyListener(new EventHandler());
        label2.addHierarchyListener(new EventHandler());
        label3.addHierarchyListener(new EventHandler());
        label4.addAncestorListener(new EventHandler());

        label1.addComponentListener(new EventHandler());
        label2.addComponentListener(new EventHandler());
        label3.addComponentListener(new EventHandler());
        label4.addComponentListener(new EventHandler());

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(tabbedPane, BorderLayout.CENTER);
        f.setPreferredSize(new Dimension(600, 400));
        f.pack();
        f.setVisible(true);
    }

    class EventHandler implements AncestorListener, ComponentListener, HierarchyListener {

        @Override
        public void ancestorAdded(AncestorEvent event) {
            System.out.println("CardlayoutTest.EventHandler.ancestorAdded()");
        }

        @Override
        public void ancestorMoved(AncestorEvent event) {
            System.out.println("CardlayoutTest.EventHandler.ancestorMoved()");
        }

        @Override
        public void ancestorRemoved(AncestorEvent event) {
            System.out.println("CardlayoutTest.EventHandler.ancestorRemoved()");
        }

        @Override
        public void hierarchyChanged(HierarchyEvent e) {
            System.out.println("Components Change: " + e.getChanged());
            if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
                if (e.getComponent().isDisplayable()) {
                    System.out.println("Components DISPLAYABILITY_CHANGED : " + e.getChanged());
                } else {
                    System.out.println("Components DISPLAYABILITY_CHANGED : " + e.getChanged());
                }
            }
            if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {
                if (e.getComponent().isDisplayable()) {
                    System.out.println("Components SHOWING_CHANGED : " + e.getChanged());
                } else {
                    System.out.println("Components SHOWING_CHANGED : " + e.getChanged());
                }
            }
        }

        @Override
        public void componentHidden(ComponentEvent e) {
            System.out.println(e.getComponent().getClass().getName() + " --- Hidden");
        }

        @Override
        public void componentMoved(ComponentEvent e) {
            System.out.println(e.getComponent().getClass().getName() + " --- Moved");
        }

        @Override
        public void componentResized(ComponentEvent e) {
            System.out.println(e.getComponent().getClass().getName() + " --- Resized ");
        }

        @Override
        public void componentShown(ComponentEvent e) {
            System.out.println(e.getComponent().getClass().getName() + " --- Shown");
        }
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MyTabbedPane frame = new MyTabbedPane();

            }
        });
    }
}