JScrollPane arrows don't update

S

Simon Andrews

I've got an odd problem when using a JScrollPane to move around a very
wide panel.

To minimise the overhead of refreshing, and to keep some elements static
in the viewport I'm only painting the region of the panel which is
visible. I'm therefore using getVisibleRect() to see which bit is
visible and then painting that.

This all works OK, and I can scroll fine using either the slider or by
doing block jumps by clicking on an empty part of the slide bar.

However, if I scroll using the arrows at the end of the bar I see my
static text move across the screen. The paint method is getting called,
and the offset values for x are being updated in the console print, it's
just that the display doesn't change (the value in the text remains as
it was before). If I minimise and restore the application then
everything jumps back to where it's supposed to be and the text has
changed, suggesting that something's not being notified when it should,
but I can't see what else I'm supposed to have done.

The code for the drawing routine (from a JPanel inside a JScrollPane) is
below.

Any suggestions are welcome...

Cheers

Simon.

public void paintComponent (Graphics g) {
super.paintComponent(g);
int fullWidth = getWidth();
int height = getHeight();
Rectangle r = getVisibleRect();
int x = r.x;
int width = r.width;

System.out.println("Track width: "+width+" height:"+height+" Offset:"+x);

g.setColor(Color.BLACK);
g.drawString("Offset is"+x,x+2,height-2);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top