Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JPanel not scrolling in a JScollPane

Status
Not open for further replies.

ahodge

Programmer
Mar 12, 2003
11
US
Hi. I'm drawing a large tree structure. The tree class extends JPanel, and I need to put it into a JScrollPane. The tree will not always be the same size every time it is drawn, and I think I need some way to tell the JScrollPane how big the tree is, in order for it to scroll. I've been told not to make the tree drawing a client of the ScrollPane, but rather just "stuff it into the ScrollPane".

Please help.

Thanks
 
You tell JScrollPane the size by setting the size of the JPanel using setPreferredSize(). If it's larger than the JScrollPane then scrolling will be activated. "When you have eliminated the impossible, whatever remains, however
improbable, must be the truth." ~ Arthur Conan Doyle
 
Thanks for the reply 'idarke', but I've already done that, and still no scrolling. I even added HORIZONTAL_SCROLLBARS_ALWAYS, and VERTICAL_SCROLLBARS_ALWAYS as policies. The scroll bars show up, but no "thumb pad", or scrolling action at all.

Any other help?

Adam
 
It works on this example. Try it and see what you get...

jScrollPane1.setBounds(new Rectangle(25, 20, 350, 235));
jScrollPane1.getViewport().add(jPanel1, null);
this.getContentPane().add(jScrollPane1, null);

jPanel1.setBackground(Color.red);
jPanel1.setPreferredSize( new Dimension(200,300) );

"When you have eliminated the impossible, whatever remains, however
improbable, must be the truth." ~ Arthur Conan Doyle
 
Thanks again for you real quick reply.

Being relatively new to Java, I'm not sure of the context of your example. I think I need your example surrounded by a bit more code, so I can just cut & paste it into a text editor, and away I go.

Thanks again.

Adam
 
Here's the complete test class.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Frame1 extends JFrame
{
private JScrollPane jScrollPane1 = new JScrollPane();
private JPanel jPanel1 = new JPanel();

public Frame1()
{
this.getContentPane().setLayout(null);
this.setSize(new Dimension(400, 601));
jScrollPane1.setBounds(new Rectangle(25, 20, 350, 235));
jScrollPane1.getViewport().add(jPanel1, null);
this.getContentPane().add(jScrollPane1, null);

jPanel1.setBackground(Color.red);
jPanel1.setPreferredSize( new Dimension(200,300) );
}
}
"When you have eliminated the impossible, whatever remains, however
improbable, must be the truth." ~ Arthur Conan Doyle
 
Thanks! I put a main() in it, and I'm fooling around with it now.

:)

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top