0

I have a JPanel inside a JScrollPane, and that JPanel must be able to change width when the users moves the mouse wheel. I wrote the event listener for the mouse wheel, which works, but the JScrollPane seems to force a different (constant) size on the inner JPanel. I found a ScrollablePanel class online, but it seems to have the same problem. How can I fix it?

Thanks :)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
dosse91214
  • 561
  • 1
  • 5
  • 17
  • using setPreferredSize on the inner JPanel seems to work, but the JScrollPane won't update the scroll bars until I click on them – dosse91214 Apr 05 '14 at 11:58

1 Answers1

1

Ok, I seem to have found the solution by pure luck, so I'm posting it here in case it's use to someone:

-set the size of the JPanel with setPreferredSize instead of setSize -use scrollPane.setViewportView(panel) every time to update the scrollbars

doesn't make sense to me, but it works, so I'm happy with it.

dosse91214
  • 561
  • 1
  • 5
  • 17
  • You need to ask for the preferred size rather than setsize, when using a layoutmanager, because you ask the layoutmanager to choose the "right position and size" according to the rules laid out by the layoutmanager – Dylan Meeus Apr 05 '14 at 12:16
  • yep, it makes sense; what does not however is that setViewportView that I have to call every single time it changes size, looks like a bug to me, maybe I should report it – dosse91214 Apr 05 '14 at 14:48