Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ordinarily, "Swing programs should override <code>paintComponent()</code> instead of overriding <code>paint()</code>," as mentioned in <a href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html#callbacks" rel="nofollow noreferrer"><em>Painting in AWT and Swing: The Paint Methods</em></a>. Based on <a href="https://stackoverflow.com/a/3518047/230513"><code>ScrollPanePaint</code></a>, which draws <em>below</em> the scrolling content, the following example overrides <code>paint()</code> to draw <em>above</em> the scrolling content.</p> <p><img src="https://i.stack.imgur.com/p3gfI.png" alt="ScrollPanePaint"></p> <pre><code>import java.awt.*; import javax.swing.*; /** * @see https://stackoverflow.com/a/10097538/230513 * @see https://stackoverflow.com/a/2846497/230513 * @see https://stackoverflow.com/a/3518047/230513 */ public class ScrollPanePaint extends JFrame { private static final int TILE = 64; public ScrollPanePaint() { JViewport viewport = new MyViewport(); viewport.setView(new MyPanel()); JScrollPane scrollPane = new JScrollPane(); scrollPane.setViewport(viewport); this.add(scrollPane); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } private static class MyViewport extends JViewport { public MyViewport() { this.setOpaque(false); this.setPreferredSize(new Dimension(6 * TILE, 6 * TILE)); } @Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.blue); g.fillRect(TILE, TILE, 3 * TILE, 3 * TILE); } } private static class MyPanel extends JPanel { public MyPanel() { this.setOpaque(false); this.setPreferredSize(new Dimension(9 * TILE, 9 * TILE)); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.lightGray); int w = this.getWidth() / TILE + 1; int h = this.getHeight() / TILE + 1; for (int row = 0; row &lt; h; row++) { for (int col = 0; col &lt; w; col++) { if ((row + col) % 2 == 0) { g.fillRect(col * TILE, row * TILE, TILE, TILE); } } } } } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new ScrollPanePaint(); } }); } } </code></pre> <p><strong>Note:</strong> Setting an opaque component (for instance <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html" rel="nofollow noreferrer">JTable</a>) as a view for the scroll pane will give strange visual bugs, for instance moving the fixed blue box when scrolling. Use setOpaque(false) on the view component to fix that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload