I have JPanel wrapped in JScrollPane and I want the rectangle to be drawn always on the same position = moving with scrollbars wont affect the visibility of the rectangle.
I tried following code:
public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.drawRect(50, (int)getVisibleRect().getY(), 20 , 20);
}
but it only repaints the rectangle when size of whole JPanel is changed.
IIRC,
JScrollPanewill try to minimise the amount of redrawing done scrolling, so it wont always cause your component to be updated.The standard technique is to use a
JLayeredPane. Add youJScrollPaneto a lower layer, and a non-opaque glass panel component above it. See How to Use a Layered Pane in the Swing tutorial.