Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scrollbar not working

 
Laxmikant Kumbhare
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me!
I tried to add scrollbar to my JPanel but its not working.
I am copying my code here:

 
John Jai
Rancher
Posts: 1776
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please format your code with code tags and better post an SSCCE and please KeepItDown
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Laxmikant Kumbhare
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yhank you Rob,
I did what you said except layout.
I am giving you my code. Please check it once again.

 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic