Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[jCrollPane]Positionning the view with scrollRectToVisible(RectangleToBeDisplayed);

896 views
Skip to first unread message

Daniel Moyne

unread,
Aug 1, 2009, 5:39:23 PM8/1/09
to
-------------------- <-mainJscrollPane with mainJpanel
| 1 | | |
|--------------| | |
|----------------| | <-jScollPaneOfJTree in JPanel
| 2 | | |
|--------------| | |
|----------------| | <-jScollPaneOfJTree in JPanel
| 3 | | |
|--------------| | |
|----------------| | <-jScollPaneOfJTree in JPanel
....................
....................

Sorry to come back with this ; I recall that I have a main Jpanel associated
to a main JScrollPane and in this mainJPanel a collection of JPanels each
containing a JTree inside and a JScrollPaneOfJtree associated to view the
JTree as desired.

Now I want to be able to display a Jpanel containing a JTree by scrolling
the mainJScrollPane.

As I said earlier I succeeded with the basic method of using a vertical
scrollBar and calculating the position position of the vertical slider.

As someone proposed to use the method ;
scrollRectToVisible(RectangleToBeDisplayed);

I did this (1) :
Rectangle RectangleToBeDisplayed = JScrollPaneOfJtree.getBounds();
then this :
mainJScrollPane.scrollRectToVisible(RectangleToBeDisplayed);
which has no effect at all on the display (claimed JPanel may stay
invisible) but does not bug.

Now if I do this instead which for me does not make sense (2) :
Rectangle RectangleToBeDisplayed = JScrollPaneOfJtree.getBounds();
JscrollPaneOfTJree.scrollRectToVisible(RectangleToBeDisplayed);

Sometimes it works other times it does not ! So it is caotic

Can the method scrollRectToVisible() be applied in the present case and can
I fix (1) to get it working.
Thanks.

Knute Johnson

unread,
Aug 1, 2009, 5:47:27 PM8/1/09
to

From the docs:

"JComponent.scrollRectToVisible

public void scrollRectToVisible(Rectangle aRect)

Forwards the scrollRectToVisible() message to the JComponent's
parent. Components that can service the request, such as JViewport,
override this method and perform the scrolling.

Parameters:
aRect - the visible Rectangle
See Also:
JViewport"

Is the parent of the JPanel a JViewport?

Go back and look at the code example I wrote for you in the previous
thread for how to do this.

--

Knute Johnson
email s/nospam/knute2009/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Roedy Green

unread,
Aug 2, 2009, 9:37:47 PM8/2/09
to
On Sat, 01 Aug 2009 23:39:23 +0200, Daniel Moyne
<daniel...@neuf.fr> wrote, quoted or indirectly quoted someone who
said :

>Sometimes it works other times it does not ! So it is chaotic

Sounds like you might be tying up the EDT thread so the event to
scroll the JScrollPane never gets handled.

Look at all your event handlers to make sure the complete very
quickly.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Patriotism is fierce as a fever, pitiless as the grave, blind as a stone, and as irrational as a headless hen."
~ Ambrose Bierce (born: 1842-06-24 died: 1914 at age: 71)

Simon

unread,
Aug 3, 2009, 5:27:53 AM8/3/09
to
> As someone proposed to use the method ;
> scrollRectToVisible(RectangleToBeDisplayed);

That might have been me.

> I did this (1) :
> Rectangle RectangleToBeDisplayed = JScrollPaneOfJtree.getBounds();
> then this :
> mainJScrollPane.scrollRectToVisible(RectangleToBeDisplayed);

Don't call scrollRectToVisible on the scroll pane. Call it on the
component you want to make visible. Recall my original answer:

> panelToMakeVisible.scrollRectToVisible(Rectangle(0, 0,
> getWidth(), getHeight());

To understand this behaviour, just read the docs.

http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#scrollRectToVisible(java.awt.Rectangle)

It propagates the call to its parent component. It is not even the
JScrollPane which eventually handles it, it is the JViewport.

Cheers,
Simon

John B. Matthews

unread,
Aug 3, 2009, 12:44:47 PM8/3/09
to
In article <7dnosuF...@mid.dfncis.de>,
Simon <count....@web.de> wrote:

Excellent! In Knute's example, replace the first with the second:

1) vp.setViewPosition(panels[n].getLocation());

2) panels[n].scrollRectToVisible(new Rectangle(0, 0, ...));

These have slightly different results when scrolling vertically in a
viewport that is taller than the panel. The former scrolls the panel to
the top of the viewport; the latter scrolls just enough to get the panel
into view.

As an alternative to implementing Scrollable, it's also possible set the
scroll bar's unit increment:

sp.getVerticalScrollBar().setUnitIncrement(...);

<http://java.sun.com/docs/books/tutorial/uiswing/components/
scrollpane.html#scrollable>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

0 new messages