• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JScrollPane

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about attaching the scroll pane to the label via the JScrollPane constructor? Perhaps that might work...
 
Cathy Young
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 560
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Steven. That makes a lot of sense.
Cathy
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic