Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Component Name

Status
Not open for further replies.

jsulman

Programmer
Jun 14, 2001
85
US
I am having some trouble getting the name for a toolBar component.

I create a jtoolBar as a separate class and instant it in a JinternalFrame. In the JinternalFrame I need to get its components and add a listener to each component. The code I am using is this:

Component toolBarComp[] = jToolBar.getComponents();
for(int i = 1; i < toolBarComp.length; i++)
System.out.println(toolBarComp.getName());

However when I print the getName method all the names are null. I do set the name for the components in the tool bar class and I can get them to print from the class. But using the getComponents() I cannot get the name to display.

Any ideas as to what I could be doing wrong? Or is there a better way of doing this.

Thanks in advance

Jeff
 
Why do you need to add a listener to each componenet in the JInternalFrame class, why not do it where they are created and managed, ie in the JToolBar class?

Or more specifically, extend the JToolbar class so that you add listeners to everthing you want and then you can add the toolbar to the JInternalFrame and bingo!.

If you need to report the action back to the JInternalFrame you could always pass the frame as a parameter to the JToolbar when you create it.
ie
Code:
myToolBar bar = new myToolBar(this);
where
Code:
this
is obviously the frame.

inside the toolbar you could have something similar too..
Code:
class myToolbar extends JToolBar
{
   JFrame frame;
   public myToolBar(JFrame f)
   {
      super();
      frame = f;
   }
}

then for every listener action you could call a procedure in the jframe, calling it via the &quot;frame&quot; variable.

does that make any sense?

Or have a totally mis-read your question?


Oh, and no I dont how how to get the componenet name. sorry :) -------------------------------------------
There are no onions, only magic
-------------------------------------------
 
jfryer,
That is an excellent idea. In fact its the way I first did it. Then I took a oo class through sun and the instructor told me not to do it that way. He said it was creating too tight a coupling between the two objects. However, I think its the way to go. Thanks for your help.

Jeff
 
if you look at Sun's source code for some of Swing they do the same as I have suggested (thats where I got the idea from :) )

The JDialog box for example.

While the coupling is tight, considering the Menubar is directly related to that Frame and only the Frame I think it is an acceptable method. -------------------------------------------
There are no onions, only magic
-------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top