2

我创建了 3 个面板。在我添加的根面板中JTabbedPane,在我添加的第二个选项卡中PanelBorderLayout最后在BorderLayout.WEST第三个选项卡中。在第三个里面我添加了一个JTreewith PopupMenu

如何在 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();
    }
});

输出:

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)
4

2 回答 2

7

您可以使用SwingUtilities.getAncestorOfClass方法。

于 2012-05-02T14:47:51.973 回答
1

我认为让父母链尝试查找特定组件来更新它不是一个好主意。如果您想将该 JTree 放在其他位置,或者只是重新构建您的布局,该怎么办。你将被迫改变所有这些事情。

您可以尝试使用 MVC。JTree 向控制器发送一个事件,该控制器将具有到 JTree 本身和根窗格的接口。当您实例化您的控制器时,将所有视图作为控制器的协作者传递。

我不知道您的面板在哪里实例化。但是,如果您在根级别实例化您的控制器应该很容易获得对您需要的所有组件的引用。

于 2012-05-02T14:48:45.983 回答