Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JScrollPane for JPanel  RSS feed

 
chris liao
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to add many buttons on a panel with gridlayout,
When the count of buttons reach some number, I hope that a JScrollPane
can appear. I have tried, but no result ! please help me !

Below is mu key source:

JPanel panel = new JPanel();
panel.setLayout(new GridLayout());
JScrollPane scroll= new JScrollPane(panel);
scroll.getViewport().add(panel, null);

//begin
for (int i=0;i<100;i++){
panel.add(new Button(""+i));

panel.validate();
 
chris liao
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me ! It is very important to me, if not solve the problem,
maybe i will lost my work!


Thanks!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's a weekend, we are not all just waiting here to pounce on every post.

read this to understand it a bit better next time you have a problem.
http://faq.javaranch.com/view?EaseUp

now, onto your problem.

the scrollbars will appear when the preferred size of the scrollpane is less
than that of it's content.

so, if the scrollpane is added directly to the frame/contentPane,
its preferred size will be determined by the size of the frame/contentPane.

if the scrollpane is added to a panel, and the panel added to the frame/contentPane,
you will need to set a preferred size for the scrollPane
scrollPane.setPreferredSize(new Dimension(100,100));

where (100,100) is anything you want.

sample code here - compile/run 3 times for the various options
http://www.coderanch.com/t/342663/GUI/java/getting-JScrollPane-display-its-scrollbars

if you still have problems, post a sample program, one that we can
copy/paste/compile/run and see it not working.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!