wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Scrollbar not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Scrollbar not working" Watch "Scrollbar not working" New topic
Author

Scrollbar not working

Laxmikant Kumbhare
Ranch Hand

Joined: Sep 19, 2011
Posts: 38
Please help me!
I tried to add scrollbar to my JPanel but its not working.
I am copying my code here:

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Please format your code with code tags and better post an SSCCE and please KeepItDown
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

JScrollPane uses the preferred size of its viewport view, not the "actual" size. Your panel has a preferred size of 0x0 as it has no layout manager to automatically calculate it. If you really want to continue using a null layout manager (which I do not recommend), then use setPreferredSize to set it explicitly.

There are also three problems with your paintComponent method:
1) It's public while it should remain protected.
2) The first call should be super.paintComponent(g); but that's missing.
3) You're modifying the frame (removing a component, repositioning that component, adding it again) from the method. You really, really shouldn't. First of all, the adding and removing is completely unnecessary if you only need to set the bounds. This should be done from the actionPerformed method itself.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Laxmikant Kumbhare
Ranch Hand

Joined: Sep 19, 2011
Posts: 38
Yhank you Rob,
I did what you said except layout.
I am giving you my code. Please check it once again.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

After a quick glance, apart from still setting the bounds in the paintComponent method I don't see much wrong. You do have several calls to setSize in your actionPerformed method, of which only the very last one will be effective; the ones before that will be caused to be discarded because of the last one.
Laxmikant Kumbhare
Ranch Hand

Joined: Sep 19, 2011
Posts: 38
Rob Spoor wrote:After a quick glance, apart from still setting the bounds in the paintComponent method I don't see much wrong. You do have several calls to setSize in your actionPerformed method, of which only the very last one will be effective; the ones before that will be caused to be discarded because of the last one.



I have solved the problem. Your suggession was useful. Thanks for that. I used setsize multiple times to refresh the frame. Because if i didn't write it I need to do it manually by resizing window.

Thanks once again!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Scrollbar not working
 
Similar Threads
Koch SnowFlake
master exam doubt
Master Exma question
flicker in my program
Recursion SnowFlake ?