Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add a JPanel to a JScrollPane

 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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. . .
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic