• 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 knob won't change size

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good morning all,

I'm in a bit of an issue here. I can't configure my JScrollPane to Resize its knob to allow scrolling when additional components are dynamically added to the JPanel it contains.
What I'm doing is adding a JPanel inside a JScrollPane using NetBeans visual designer. Then I have a Button whose event adds an n-number of JCheckBoxes one below the last one (vertically) at runtime when I click the button. As you will notice in the ScreenShot the problem is that I can't see but only the first 5 JCheckBox(es) added to the JPanel.

I expected the JScrollPane to allow me to view the rest of the JPanel by Scrolling it, but I can't get it to work in that way.

Here's the code for the Button Event, and some of the NetBeans Design generated code:



Please Help me ! What I'm missing that prevents my JPanel to be Scrolled ?

Thank you very much as Always !

Best Regards,

Jose.
Result.jpg
[Thumbnail for Result.jpg]
This is how the JFrame looks after the Button is clicked.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is the NetBeans generated code which uses a GroupLayout.

You can't just use panel.add(...). You also need to specify a bunch of constraints for the GroupLayout.

My advice is to scrap the NetBeans layout code and do the layout yourself.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings Rob !
First of all Thanks,

ok, Which Layout do you suggest using ? apart from that, Once I Arrange my Layout properly could I just do something like:



And Expect to get the desired results ?

Thank you very much Sir !

Jose.


 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the section from the Swing tutorial on Laying Out Components in a Container. You might use a BoxLayout or a GridLayout. You may also need to nest panels to get the desired layout.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will be done Sir !

Thanks for your recommendations. I'll proceed accordingly. A minute ago I was thinking about the Panel nesting myself, so, that's exactly what I'll do.

Have a nice day Mr. Camick
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Again !

I'm returning with yet another battle scar, I did all the GUI by hand to no avail. Actually, The Results are exactly the same. I can not see all the JCheckBox(es) I'm adding, I'll continue trying to get it to work by reading further, but for now I'd thank if someone takes a look at this 'Ready to Bake' Code. Just Copy/Paste it, and execute it...



What's Wrong with my ScrollPane ? Please Help me....

Best Regards,

Jose.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jose Campana wrote:

What's Wrong with my ScrollPane ?


The scroll pane uses the preferred size of its viewport component. Usually the layout manager takes care of that but you don't use one. That's in 99.9999999999999999999% of the cases a bad idea. Wait, make that 100%*. Also, if you call setPreferredSize, that will always take precedence of the preferred size as calculated by the layout manager.

Try reading that layout manager tutorial again, and abandon using a null layout and calling setBounds yourself. You will see that, with the proper layout manager, the preferred size will be recalculated properly and your application will work.


* Even if you need to position and size components manually you would still need a layout manager to calculate the preferred and minimum sizes.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So much for suggesting a BoxLayout or GridLayout. I would think that would be the first section you read and try.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Rob, nice to read you,

Before proceeding to study about the Layout Managers once again, Let me just mention that the code I previously showed was indeed using a Layout Manager. Even though it's a NetBeans generated code, it used Layout Managers nonetheless. So, what's going on there?

ok, I'll post back once I have results.

Thank you for your time Rob. You're the best !

Good Luck,

Jose.

And to Mr. Camick, I'm studying those APIs right away Sir ! The last code I posted was just a rough attempt using what I remembered.... I'm always open for suggestions, ... thanks again.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Day,

Frustration has descended upon me this morning, In my latest attempt as you will soon see, I have not yet been able to achieve the desired behaviour.
There's something that's obviously terribly wrong, but due to the lack of an example that points me in the right direction, I come to you guys to the rescue. Here's my code, that uses Layouts as suggested previously:



This Question may sound annoying at this point, but..... What am I doing wrong ? What's really going on with the JScrollPane ?

Please help me,

Sincerely,

Jose.

PS. I think this is the time where a little code on your part would come super handy. I hope you don't mind to illustrate me.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You where given a link to the Swing tutorial for reading up on layout managers. Did you also read the section on "How to Use Scroll Panes"?

Please show me where in that tutorial (or any other scroll pane example on the web) you see the above line of code. People make things harder than they should be because they add lines of code they don't understand. If you don't see it in an example chances are you don't need it.


In a previous reply Rob said:

with the proper layout manager, the preferred size will be recalculated properly and your application will work



So why are you setting the preferred size?
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there !

Now I realize the error of my ways... the funny things is.. before you posted your reply, I had already solved it.. like this:



As you can see, I started to think about the preferred size property and put that exercise to the test. However your solution is WAY better, I suppressed the setPreferredSize altogether and it works too ! In a cleaner way also.
So, Thank you for the lesson. I'll try to follow the tutorials in the future more accordingly.

Have a Nice day.

jose.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic