• 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

Add a JPanel to a JScrollPane

 
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I have an object that inherits from JPanel. I would like to add this JPanel to a JScrollPane in another class. Everything works fine when I do not add this panel to the scrollpane, but when I do, no information is present and it is, or is as if the panel does not exist. Here is the bit of code that I am using . . . .



I do not get any exception thrown. Any help or suggestion are greatly appreciated.

Thank you

Greg
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not the way to use a JScrollPane. Read the API and follow the link to the tutorial on How to Use Scroll Panes, where you will find vcode examples.
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the JScrollPane exactly how Ive seen it used on multiple tutorial sites. Rather than just saying it is wrong, maybe you could tell me HOW I am using it wrong. Then I would also have a frame of referance during the search to correct the errors. Thanks.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, Darryl did suggest you taking a look at http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, avoid any tutorial site that recommends using the add(...) method of a scroll pane.
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it to work by putting the panel directly into the contructor for the JScrollPane, but I still would like to know why it did not work the add method, afterall, what else would an add method be used for. . .
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The add methods are inherited from java.awt.Container, and the addImpl method is inherited from javax.swing.JComponent. These should be used for internal use only. To add something to a JScrollPane after it's been constructed you should use its JViewport instead. Directly:
Indirectly:

But I agree that perhaps JScrollPane should override the methods for adding and removing components, to delegate these calls to the JViewport. The Sun UI classes have an overall problem with guarding against unwanted adding / removing of components. When I create a JComponent subclass I usually make sure that it's not possible to add or remove components, or to set the layout, in any way that's not intended.

Part of the problem here is that JComponent extends Container, and that's wrong in my opinion. It means that you can technically set the layout or add child components to any JComponent, including those like JLabel, JTextField and JButton. I doubt that's intended. Sun should have made JComponent extend Component, and copied the code of Container to a new class (JContainer comes to mind). Classes like JLabel should then still extend JComponent, whereas JPanel and other classes that actually should be containers for other components should extend JContainer instead.
 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the information and explaining this to me. One problem with trying to learn how to program alone, and without the help of teachers, is that you never learn stuff like this. Thanks a lot!

Greg
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic