How to draw on JPanel on fixed position?
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, JScrollPane will 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 you JScrollPane to a