wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes JScrollPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "JScrollPane" Watch "JScrollPane" New topic
Author

JScrollPane

Cathy Young
Ranch Hand

Joined: Mar 02, 2002
Posts: 126
Hi, I am confused need your help.
I have the following code segment:
public class TestClass extends JFrame {
public TestClass () {
...
JLabel testLabel ("This is a test");
getContentPane ().add (testLabel, BorderLayout.South);
setVisible (true);
}
...
}
It works fine, but if I add the label to a JScrollPane, and then add the JScrollPane to the Frame as follows, I can not see anything.
JLabel testLabel = new JLabel ("This is a test");
JScrollPane testPane = new JScrollPane ();
testPane.add (testLabel);
getContentPane ().add (testPane, BorderLayout.South);
Can someone tell me why? Is there anything special I need to do for JScrollPane? Thanks!
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
I don't know the reason. But why do you want to add a JLabel to a JScrollPane? Normally JScrollPane is used to add JTable or JTextArea where the size can grow dynamically.
May be call setPreferredSize(width,height) on the JScrollPane and try again.
Steven Sloggett
Greenhorn

Joined: Feb 11, 2002
Posts: 15
How about attaching the scroll pane to the label via the JScrollPane constructor? Perhaps that might work...


Steven
Cathy Young
Ranch Hand

Joined: Mar 02, 2002
Posts: 126
I don't know the reason. But why do you want to add a JLabel to a JScrollPane? Normally JScrollPane is used to add JTable or JTextArea where the size can grow dynamically.

Thanks, Sai. The reason I add a JLabel to JScrollPane is that I failed to add JTable to JScrollPane and JScrollPane to JFrame, so I want to test what is the problem and it did not work.
May be call setPreferredSize(width,height) on the JScrollPane and try again.
I tried this, but it does not work either.
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
Cathy,
I think I know the problem. You are not setting the layout for the ContentPane of the JFrame.
Try the code below in the JFrame constructor:
Cathy Young
Ranch Hand

Joined: Mar 02, 2002
Posts: 126
Hi, Steven and Sai,
Thank you for your replies. Your solutions work! But I still could not understand why mine failed (unless I have to pass the component to be added as parameter of the constructor of JScrollPane). Sai, I did set the layout manager although I did not include in the code.
Another question arises at the meantime: what if I want to add more than one component into a JScrollPane? What I am thinking now is to add them into another component, eg, JPanel, and then add this component to JScrollPane. Isn't this too ugly? Any better solution?
Thanks!
Cathy
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
It is not ugly. But again, I've never added a JPanel to the JScrollPane. I make all my components visible and so no need to use the JScrollPane to display gui components like JButton, JLabel and JTextField.
[ May 12, 2002: Message edited by: Sai Prasad ]
Steven Sloggett
Greenhorn

Joined: Feb 11, 2002
Posts: 15
Originally posted by Cathy Young:
Thank you for your replies. Your solutions work! But I still could not understand why mine failed (unless I have to pass the component to be added as parameter of the constructor of JScrollPane). Sai, I did set the layout manager although I did not include in the code.

When I looked at the API for JScrollPane, there was no 'add' method for JScrollPane specifically - it is not overridden for the JScrollPane class. That is why I suggested using the JScrollPane constructor which is guaranteed to do what you require (or you could use the 'setViewportView' method).
'add' implies that there can be multiple components - whereas JScrollPane only ever deals with one component directly.

Another question arises at the meantime: what if I want to add more than one component into a JScrollPane? What I am thinking now is to add them into another component, eg, JPanel, and then add this component to JScrollPane. Isn't this too ugly? Any better solution?

I agree with Sai. I think it's fine to use JScrollPane this way if required.
Cathy Young
Ranch Hand

Joined: Mar 02, 2002
Posts: 126
Thanks, Steven. That makes a lot of sense.
Cathy
 
 
subject: JScrollPane