File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT / JFace and the fly likes Retrieving a Component from a SplitPane Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "Retrieving a Component from a SplitPane" Watch "Retrieving a Component from a SplitPane" New topic
Author

Retrieving a Component from a SplitPane

Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
I am trying to write my first real Swing Application. I have a JFrame with a JMenuBar, JToolBar, and a JSplitPane. I have JToolTips, JMenuItems, ActionListeners, the works.
Now I want one of the action listeners to update a JLabel on a JPanel on the left side of the JSplitPane.
I have found the JSplitPane.getLeftComponent() method but I seem to be at a loss as to how I actually make it work. I can't instantiate a Component, but if I try:

the compiler says that they are incompatible types.
I'm sure this is a simple question, like casting or something, but I am simply stumped.
After I accomplish this, how do I get my JLabel out of the JPanel and update it?
All the books show you how to build this stuff but don't seem to do a very good job telling you how to manipulate it once it's out there!
Thank in advance,


Wait a minute, I'm trying to think of something clever to say...<p>Joel
cardwell cupp
Ranch Hand

Joined: Aug 16, 2001
Posts: 66
Sounds like you are having fun! I believe that a JPanel extends from a JComponent which also extends from a Component, so you could type cast it:
JPanel left = (JPanel)splitPane.getLeftComponent();
But I see that you want to get a JLabel out of the left pane. Where did you put the JLabel?
If you instaniated it and it is member of you class, you can just acces it that way. I am not sure how to get it otherwise...?
Did you ?
private JLabel label = new JLabel ("text");

or

leftPane.add(new JLabel("text"));
Cardwell
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
Thanks CC, that did it!
I knew it had to be something simple... I think by the time I wrote the message I was severly brain fried!
As far as the updating, I may have to simply clear the existing JLabel and replace it with a new one. Here is the code used to create the splitPane...

Now that I have retrieved the leftComponent that I would be able to change it's properties? I may not be using the right components yet, just started seriously messing with this stuff on Tuesday.
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
Jus tin case anyone is curious I found a far simpler way to accomplish this.
If I define every component as a private instnace variable, then I never have to worry about retrieving a component from within another component to update it, I need only refer to it directly...
 
 
subject: Retrieving a Component from a SplitPane
 
Threads others viewed
JDialog sizing
JSplitPane Problem ...
view before and after Tsunami
JSplitPane not moving divider
JSplitPane and its setDividerLocation method
IntelliJ Java IDE