• 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

JScrollPane for JPanel

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
https://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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic