• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1073
  • Last Modified:

How to get JInternalFrame form its contentPane??

Hi,
   I have a JInternalFrame in my application and am adding a toolbar and JPanel to its contentPane.
   I want to get to the JInternalFrame from the JPanel added to the contentPane. I am using getParent() and am able to get to the contentPane successfully but I'm not able to get to the JInternalFrame.
   Any help would be appreciated.Thanks in advance.
Abhishek.
0
abhishekroy
Asked:
abhishekroy
  • 3
  • 3
1 Solution
 
schybertCommented:
You could use:
panel.getParent().getParent().getParent().getParent()

but there has to be a better way :-)
0
 
abhishekroyAuthor Commented:
Nopes! Doesn't work.
0
 
OviCommented:
Here is a little application I've done for you :

As a shortcut, the detection code is the following from the method createInternalFrame():

            final JTextField result = new JTextField(40);
            final JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createTitledBorder("Internal JPanel"));
            
            JButton searchParent = new JButton("Search Parent");
            searchParent.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent ae) {
                        Container parent = panel;
                        while(!(parent instanceof JInternalFrame) && parent != null)
                          parent = parent.getParent();
                        result.setText((parent == null) ? "Parent not found" : "Founded parent: " + parent.getName());
                              
                  }
            });





/*
 * Created on Feb 19, 2003
 *
 * To change this generated comment go to
 * Window>Preferences>Java>Code Generation>Code Template
 */

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class AccesingParent extends JFrame {
      JDesktopPane desktop;
      JButton add;
      
      public AccesingParent() {
            initLayout();
            initListeners();
      }
      
      private void initLayout() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            desktop = new JDesktopPane();
            
            add = new JButton("Add InternalFrame");
            JPanel p = new JPanel();
            p.add(add);
            
            Container c = getContentPane();
            c.setLayout(new BorderLayout());
            c.add(desktop, BorderLayout.CENTER);
            c.add(p, BorderLayout.SOUTH);
      }

      private void initListeners() {
            add.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent ae) {
                    JInternalFrame internal_frame      = createInternalFrame();
                    int count = desktop.getAllFrames().length;
                    String name = "InternalFrame " + count;
                    internal_frame.setTitle(name);
                    internal_frame.setName(name);
                    internal_frame.setLocation(count * 10, count * 10);
                    desktop.add(internal_frame);
                    internal_frame.setVisible(true);
              }
            });
      }
      
      private JInternalFrame createInternalFrame() {
            JInternalFrame internal = new JInternalFrame();
            final JTextField result = new JTextField(40);
            final JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createTitledBorder("Internal JPanel"));
            
            JButton searchParent = new JButton("Search Parent");
            searchParent.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent ae) {
                        Container parent = panel;
                        while(!(parent instanceof JInternalFrame) && parent != null)
                          parent = parent.getParent();
                        result.setText((parent == null) ? "Parent not found" : "Founded parent: " + parent.getName());
                              
                  }
            });
            
            JPanel p = new JPanel();
            p.setLayout(new BorderLayout());
            p.add(searchParent, BorderLayout.WEST);
            p.add(result, BorderLayout.CENTER);
            
            Container c = internal.getContentPane();
            c.setLayout(new BorderLayout());
            c.add(panel, BorderLayout.CENTER);
            c.add(p, BorderLayout.SOUTH);
            
            internal.setClosable(true);
            internal.setResizable(true);
            internal.setSize(350, 150);
            return internal;
      }

      public static void main(String[] args) {
        AccesingParent ap = new AccesingParent();
        ap.setSize(800, 600);
        ap.setVisible(true);      
      }
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
OviCommented:
Tip: the same algorithm is used for detection of top most Frame when you'll create a modal Dialog or JDialog.
0
 
abhishekroyAuthor Commented:
Thanks that works!
0
 
OviCommented:
you're welcome
0
 
abhishekroyAuthor Commented:
Hey!
The above code works but not for my case.
I have a class WANView which extends JInternalFrame.

I am using the following code for accessing the JInternalFrame

Container parent = WANPanel.this;

/* WANPanel extends JPanel
 * An instance of WANPanel is added to the WANView Frame's
 * contentPane.
 */

while(!(parent instanceof WANView) && parent != null)
parent = parent.getParent();
WANView parentFrame = (WANView) parent;
parentFrame.toggleEditMode(false);

It's not working and throws exceptions.
What's the mistake I am making?
Please help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now