Scrolling to a component

I have a JPanel that contained withn a JScrollPane (called flowchartPane). The JPane contains other JPanels that are arranged with a grid layout.

These JPanels contain graphics and when put together in various combinations make a flowchart.

The actual size of the JPane is larger than the viewport of the JScrollPane. So there are times when I want to scroll to a perticular component that is out of the bounds of the view port rectangle.

I am aware of the scrollRectToVisible method and have been utilising it in my method. However my method seems to be unreliable.

What the method attempts to do is, if a component (comp) has its left, right, top or bottom edge outside the bounds of the viewport scroll the viewport so the component is in view.

However this method is not reliable and does not allways do what is expected. Can anyone think of anyway I can improve it, see what I am doing wrong or help in the writing of a scroll to component method.

Kind regards

Andrew Scott.

publicvoid scrollToComponent( Component comp)

{

int x = comp.getLocation().x;//get x location of the component

int y = comp.getLocation().y;//get the y location of the component

int h = comp.getHeight();//get the height of the component;

int w = comp.getWidth();//get the width of the component

Rectangle vr = flowchartPane.getViewport().getViewRect();

int newY = vr.x;

int newX = vr.y;

//if outside the bounds of the viewport

if ( ((x+w) > (vr.x + vr.width)) | (x < vr.x) |

((y+h) > (vr.y + vr.height)) | (y < (vr.y)))

{

//Do not attempt to scroll to an posision zero or less on the X axis

if (x-w > 0)

newX = x;// - w ;

else

newX = x;

//Do not attempt to sctoll to a position zero or less on the Y axis

if (y - (h/2) > 0)

newY = y;// - (h/2);

else

newY = y;

flowchartPane.getViewport().scrollRectToVisible(new Rectangle(newX,newY,vr.width,vr.height));

}//end if

}//End Scroll to Component

[3007 byte] By [scottie_uka] at [2007-11-27 8:07:32]
# 1
Try:flowchartPane.getViewport().scrollRectToVisible(comp.getBounds());If you want further help post a Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the problem. http://homepage1.nifty.com/algafield/sscce.html
Rodney_McKaya at 2007-7-12 19:50:16 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for your help Rodney.

That method gets the vertical position correct, but fails to get the horisontal position correct.

Any further ideas?

I can't really post a short executable, my prgram relies on over 50 classes spread over 12 or so packages. The naturer of the program would make this unfeasable.

Andrew.

Message was edited by:

scottie_uk

scottie_uka at 2007-7-12 19:50:16 > top of Java-index,Desktop,Core GUI APIs...
# 3

Cente of view rect must be center point of your component.

So

int centerX=comp.getBounds().getX()+comp.getBounds().getWidth()/2;

int centerY=comp.getBounds().getY()+comp.getBounds().getHeight()/2;

The rect passed in scrollRectToVisible() size (width and height) must be the same as vr size.

And we should define proper x and y.

rect.x=centerX-vr.getWidth()/2;

rect.x=centerY-vr.getHegiht()/2;

Regards,

Stas

StanislavLa at 2007-7-12 19:50:16 > top of Java-index,Desktop,Core GUI APIs...
# 4

Stanislav,

Close, but no cigar. I will say that is the best solution yet, but its far from perfect.

It does not want to scroll upwards or to the left. It will only scroll to the right or down.

Why is that?

From your ideas Stanislav I developed this method:

public void scrollToComponent( Component comp)

{

Rectangle vr = flowchartPane.getViewport().getViewRect();

int centerX=comp.getBounds().x + comp.getBounds().width/2;

int centerY=comp.getBounds().y + comp.getBounds().height/2;

int newX=centerX-vr.width/2;

int newY=centerY-vr.height/2;

flowchartPane.getViewport().scrollRectToVisible(new Rectangle(newX,newY,vr.width,vr.height));

}//end scroll to component

Kind Regards.

Andrew.

Message was edited by:

scottie_uk

scottie_uka at 2007-7-12 19:50:16 > top of Java-index,Desktop,Core GUI APIs...
# 5

Strangely is I substitue scrollRectToVisible(new Rectangle(newX,newY,vr.width,vr.height)); withflowchartPane.getViewport().setViewPosition(new Point(newX,newY)); It works but is very jumpy and not very smooth.

How can I makeflowchartPane.getViewport().setViewPosition(new Point(newX,newY)); as visually smooth as scrollRectToVisible(new Rectangle(newX,newY,vr.width,vr.height));

Andrew.

scottie_uka at 2007-7-12 19:50:16 > top of Java-index,Desktop,Core GUI APIs...
# 6
> It does not want to scroll upwards or to the left. It will only scroll to the right or down.Yes, this is a problem I've noticed as well.Try using viewport.setViewPosition(...);
camickra at 2007-7-12 19:50:16 > top of Java-index,Desktop,Core GUI APIs...
# 7
Camickr,Thank for your reply.I am now using:viewport.setViewPosition(...); But it's rather jerky and not smooth at all when compared with the other way.How can I smooth out the redraws when using your suggested method? Andrew.
scottie_uka at 2007-7-12 19:50:16 > top of Java-index,Desktop,Core GUI APIs...
# 8

> But it's rather jerky and not smooth at all when compared with the other way.

Its not a problem with the method. Its a problem with something in your code.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 19:50:16 > top of Java-index,Desktop,Core GUI APIs...
# 9
That does not explain why viewport.setViewPosition(...); is Jerky butviewport.scrollRectToVisible(...);is not.What is scrollRectToVisible doing that setViewPosition is not? Andrew.
scottie_uka at 2007-7-12 19:50:16 > top of Java-index,Desktop,Core GUI APIs...