how to get parent panel in java?

萝らか妹 提交于 2020-01-04 09:11:08

问题


I have created 3 panels. In the root panel I have added JTabbedPane, inside one of the tabs I have added second Panel with BorderLayout and finally in the BorderLayout.WEST the 3rd one. Inside the 3rd one I have added a JTree with PopupMenu.

How can I get root panel inside actionPerformed of PopupMenu?

menuItem.addActionListener(new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        System.out.println("one:"+tree.getParent().getName());
        System.out.println("two:"+tree.getParent().getParent().getName());
        System.out.println("three:"+tree.getParent().getParent().getParent().getName());
        new UnzipFile(file, file.getParentFile(),
        (rootPanel) tree.getParent().getParent().getParent() ).run();
    }
});

output:

Exception in thread "AWT-EventQueue-1" java.lang.ClassCastException: de.util.scanners.view.FileTreePanel
at de.util.scanners.view.FileTreePanel$2$1.actionPerformed(FileTreePanel.java:109)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.AbstractButton.doClick(AbstractButton.java:302)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1050)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1091)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3129)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
one:null
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
two:null
three:null
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at be.azvub.webutil.gui.WebEventQueue.dispatchEvent(WebEventQueue.java:34)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

回答1:


You could use the SwingUtilities.getAncestorOfClass method.




回答2:


I think is not a good idea get the chain of parents trying to lookup for a specific component to update it. What if you want to put that JTree in other place, or simply restructure your layout. You will be forced to change all of that things.

You could try with MVC. The JTree sends an event to the Controller that will have interfaces to the JTree itself and to the root pane. When you instantiate your controller pass all views as collaborators of the controller.

I don't know where your panels are instantiated. But if you instantiate your controller at root level should be easy get references to all components you need.



来源:https://stackoverflow.com/questions/10415963/how-to-get-parent-panel-in-java

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