Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

Finding reference to specific component

Thread view: 
Rhino - 13 Feb 2005 01:39 GMT
I am trying to determine the best way to find a reference to a specific
component on a GUI.

I have a JFrame called MyFrame. MyFrame invokes a JDialog called MyDialog
and passes a reference to itself to MyDialog so that MyDialog can find out
anything it wants to know about MyFrame.

MyFrame has three JPanels in its content pane, which are laid out with
BorderLayout. The Center panel is called MyCenterPanel. MyCenterPanel
contains only one thing, a JTabbedPane called MyTabbedPane.

I need a reference to MyTabbedPane. How do I get it?

I've already figured out how to get the components that make up MyFrame's
content pane, where parent is a reference to MyFrame:

       Component[] contentPaneComponents =
parent.getRootPane().getContentPane().getComponents();

But this is where things start to get foggy. In order to get the JPanel that
is in the Center of the layout, I used Component.setName() to give each of
the direct components of the content pane a unique name. For instance, I did
this for the Center panel of the content pane:

JPanel centerPanel = new JPanel();
centerPanel.setName("Center Panel");

That enables me to find it fairly easily by doing this:

Component centerPanel = null;
for (int ix=0; ix<contentPaneComponents.length; ix++) {
 if (contentPaneComponents[ix].getName().equals("Center Panel")) {
    centerPanel = contentPaneComponents[ix];
}
}

I must say, this feels like a bit of a hack so if there is a better way to
find the Center panel, I'd love to hear it.

At this point, I'm very close to what I want but I can't figure out how to
get there. I have a reference to the Component that holds MyTabbedPane but I
don't see how to get to MyTabbedPane from my CenterPanel reference. As a
component, CenterPanel has no getComponents() method. It has two
getComponentAt() methods but those require either an x,y coordinate or a
point on the component which I don't really have.

Okay, I could give use (0, 0) because in this case, I know that MyTabbedPane
is the only component on CenterPanel and is presumably at (0, 0) but that
seems like a hack: what if MyTabbedPane *WASN'T* the only component on
CenterPanel - how would I find it?

It seems to me there must be some better way to loop through the components
of CenterPanel but I'm darned if I know what it is. Even if I use setName()
to give MyTabbedPane a unique name, how do I get the component from its
parent, CenterPanel?

I have a feeling that I'm going about this the wrong way and would
appreciate some guidance from the GUI experts.

Signature

Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare

Bill Tschumy - 13 Feb 2005 02:22 GMT
> I am trying to determine the best way to find a reference to a specific
> component on a GUI.
[quoted text clipped - 54 lines]
> I have a feeling that I'm going about this the wrong way and would
> appreciate some guidance from the GUI experts.

I think the most straightforward way is to have MyFrame implement
getTabbedPane().  It would in turn call the getTabbedPane() method that
MyCenterPanel would implement.  Of course this requires MyFrame keep an
instance variable for the MyCenterPanel object and MyCenterPanel keeeps one
for the tabbed pane.

I'm sure there is some more "elegant" design pattern that could be used, but
I vote for KISS.

Signature

Bill Tschumy
Otherwise -- Austin, TX
http://www.otherwise.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.