aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JScrollPane knob won't change size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JScrollPane knob won Watch "JScrollPane knob won New topic
Author

JScrollPane knob won't change size

Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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.



[Thumbnail for Result.jpg]

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
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

Joined: May 28, 2007
Posts: 339
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
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
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

Joined: May 28, 2007
Posts: 339
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

Joined: May 28, 2007
Posts: 339
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19792
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
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

Joined: May 28, 2007
Posts: 339
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

Joined: May 28, 2007
Posts: 339
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
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8


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

Joined: May 28, 2007
Posts: 339
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JScrollPane knob won't change size