监听JComponent显示事件

    很多时候,我们需要在界面初始化以后对程序进行某些设置,举个例子,当界面呈现出来以后,设置下SplitPane的的百分比(关于这个为什么必须这么处理请参考java源代码)。很容易我们就想到给程序添加ComponentListener监听,然后在监听中作处理,如下所示

panel.addComponentListener(new ComponentAdapter() {

                     public void componentShown(ComponentEvent e) {

                            System.out.println("panel:shown");

                     }

              });

但是并没有和我们想象的那样,当组件在界面上显示出来的时候firecomponentShown类型的事件的,下面我们通过研究源代码分析下原因

首先的问题是在什么地方会fireComponentEvent

通过查看源代码,我们可以看到实在Componentshow()方法中

ComponentEvent e = new ComponentEvent(this,ComponentEvent.COMPONENT_SHOWN);

Toolkit.getEventQueue().postEvent(e);

具体代码,请参考java源程序

也就是说只有在组件调用到setVisible(true)的时候才会fireComponentEvent

但是你会发现,即便是我们调用JComponentsetVisible(true),也不会监听到shown事件

原因主要是JComponent重载了setVisible方法

如下

public void setVisible(boolean aFlag) {

        if(aFlag != isVisible()) {

            super.setVisible(aFlag);

            Container parent = getParent();

            if(parent != null) {

                Rectangle r = getBounds();

                parent.repaint(r.x,r.y,r.width,r.height);

            }

           // Some (all should) LayoutManagers do not consider components

           // that are not visible. As such we need to revalidate when the

           // visible bit changes.

           revalidate();

        }

}

因为JComponentvisible属性默认就是true,所以不会调用到ComponentsetVisible方法,所以也不会fireComponentEvent,当然,如果你调用下JComponentsetVisible(false)+setVisible(true)是会fireComponentEvent.COMPONENT_SHOWN事件的,(但是在里面很可能得不到当前组件的大小信息或者不能SplitPane设置分割比例,因为这个时候界面还没有初始化),而且如果是JFrameJDialogJApplet等也是没问题的,因为他们调用的都是ComponentsetVisible方法(所以给这些顶层组件添加ComponentListener是没有问题的,都会监听到)

如果不想使用上述方法,还有另外一种方式就是添加HierarchyListener监听,因为当顶层组件(JFrame,JDialog)setVisible(true)界面显示的时候,里面都会fireHierarchyEvent,具体代码如下(详细代码请参考Componentshow()方法)

       createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,

                                     this, parent,

                                     HierarchyEvent.SHOWING_CHANGED,

                                          Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));

createHierarchyEvents方法会在每个组件上fireHierarchyEvent,至于具体细节,有时间的可以dubug下,具体的解决方法如下

       panel.addHierarchyListener(new HierarchyListener() {

                     public void hierarchyChanged(HierarchyEvent e) {

                            if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {

                                   if (e.getComponent().isShowing()) {

                                          System.out.println(panel.getBounds());

                                          System.out.println("panel:hshow");

                                   }

                                   else {

                                          System.out.println("panel:hhide");

                                   }

                            }

                     }

              });

测试代码如下
public   class  Test  {
    
public static void main(String[] args) {
        
new Test();
    }


    
public Test() {
        
final JPanel panel = new JPanel();
        panel.addComponentListener(
new ComponentAdapter() {
            
public void componentShown(ComponentEvent e) {
                System.out.println(panel.getBounds());
                System.out.println(
"panel:shown");
            }

        }
);
        
//        panel.setVisible(false);
        
//        panel.setVisible(true);
        panel.addHierarchyListener(new HierarchyListener() {
            
public void hierarchyChanged(HierarchyEvent e) {
                
if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0{
                    
if (e.getComponent().isShowing()) {
                        System.out.println(panel.getBounds());
                        System.out.println(
"panel:hshow");
                    }

                    
else {
                        System.out.println(
"panel:hhide");
                    }

                }

            }

        }
);

        JFrame frame 
= new JFrame();

        frame.addComponentListener(
new ComponentAdapter() {
            
public void componentShown(ComponentEvent e) {
                System.out.println(
"frame:shown");
            }

        }
);

        frame.setContentPane(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(
500400);
        frame.setVisible(
true);
    }

}

你可能感兴趣的:(监听JComponent显示事件)