Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get parent panel in java?

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)
like image 849
itro Avatar asked Jul 19 '26 17:07

itro


2 Answers

You could use the SwingUtilities.getAncestorOfClass method.

like image 144
erikxiv Avatar answered Jul 22 '26 07:07

erikxiv


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.

like image 32
Israel Avatar answered Jul 22 '26 07:07

Israel