This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Add a JPanel to a JScrollPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Add a JPanel to a JScrollPane" Watch "Add a JPanel to a JScrollPane" New topic
Author

Add a JPanel to a JScrollPane

Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

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

Joined: May 03, 2008
Posts: 4523
    
    5

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.

luck, db
There are no new questions, but there may be new answers.
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

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

Joined: Jul 26, 2007
Posts: 10171
    
    8

Well, Darryl did suggest you taking a look at http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Also, avoid any tutorial site that recommends using the add(...) method of a scroll pane.
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Add a JPanel to a JScrollPane
 
Similar Threads
adding JPanel to a JScrollPane
Adding Scrollbar to JPanel
scroll pane in jpanel help
Adding JScollPane into JPanel
Panel in ScrollPane