Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 22584
122
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
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: 22584
122
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
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: 22584
122
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
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic