• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Scrollbar not working

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Rancher
Posts: 1776
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please format your code with code tags and better post an SSCCE and please KeepItDown
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic