• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JScrollPane & JPanel dynamic changes

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm trying to implement a JScrollPane into a JPanel. The JPanel in question handles creation of various shapes on the screen which can be dragged. I want the JScrollPane to adjust it's ScrollBars so the components dragged out of view could be seen by moving the scroll bars. I have tried this:
drawPanel = ... some JPanel
JScrollPane scrollablePanel = new JScrollPane();
scrollablePanel.getViewport().add(drawPanel);
// Make the scrollbars always appear scrollablePanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollablePanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
This simply creates the scroll bars which i cannot move. Maybe i need some kind of listeners which determine the size of the scrollable client. Can anyone please help? Thanks in advance!
Phil
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this example... I think it does what you want... one tricky thing I had to do was call getParent().validate() whenever the label is dragged and the panel needs to resize. Otherwise the scrollpane won't redraw and relayout everything, and the panel will look like it hasn't changed size.

 
Phil Lesh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the example! I really appreciate it!
Phil
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though this post is really old, but I hope someone would catch it up. The tricks for creating JScrollPane dynamically for JPanel works while dragging a JLabel around the panel. But it seems not working if multiple JLabels require scroll pane to be active both vertically and horizontally.

When one JLabel makes horizontal scroller active, if another label is dragged vertically, vertical scroller does not adjust. Keeping 2nd JLabel outside view port, if 1st label is dragged back to normal view (horizontal scroll disappears), then vertical scroll appears and 2nd label is visible. Seems each element of container is not getting attention from JScrollPane.

Any help would be appreciated.
 
Ahsan Habib
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems I found the problem what was causing scrolling problem. Rather than overriding getPreferredSize and getSize, I copied the code of getPreferredSize
in mouseDragged event handler and called scrollRectToVisible, and setPreferredSize on the panel. This takes care of scrolling adjustment.

Thanks.
 
You got style baby! More than this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic