Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes JScrollPane & JPanel dynamic changes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JScrollPane & JPanel dynamic changes" Watch "JScrollPane & JPanel dynamic changes" New topic
Author

JScrollPane & JPanel dynamic changes

Phil Lesh
Greenhorn

Joined: Oct 16, 2002
Posts: 17
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
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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.



-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Phil Lesh
Greenhorn

Joined: Oct 16, 2002
Posts: 17
Thanks a lot for the example! I really appreciate it!
Phil
Ahsan Habib
Greenhorn

Joined: Sep 17, 2008
Posts: 22
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
SCJP5, SCWCD5
Ahsan Habib
Greenhorn

Joined: Sep 17, 2008
Posts: 22
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JScrollPane & JPanel dynamic changes