?
Solved

How to get JInternalFrame form its contentPane??

Posted on 2003-02-18
7
Medium Priority
?
1,017 Views
Last Modified: 2013-11-23
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
Comment
Question by:abhishekroy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:schybert
ID: 7980161
You could use:
panel.getParent().getParent().getParent().getParent()

but there has to be a better way :-)
0
 

Author Comment

by:abhishekroy
ID: 7980347
Nopes! Doesn't work.
0
 
LVL 9

Accepted Solution

by:
Ovi earned 80 total points
ID: 7980440
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:Ovi
ID: 7980507
Tip: the same algorithm is used for detection of top most Frame when you'll create a modal Dialog or JDialog.
0
 

Author Comment

by:abhishekroy
ID: 7988109
Thanks that works!
0
 
LVL 9

Expert Comment

by:Ovi
ID: 7991878
you're welcome
0
 

Author Comment

by:abhishekroy
ID: 7992175
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question