Java
Swing
revalidate
repaint
GUI

Java Swing revalidate() vs repaint(): when to call each

Codemia Knowledge Hub ·

revalidate() tells the layout manager to recalculate component sizes and positions. repaint() schedules the component to be redrawn on screen. When you add, remove, or resize components you usually need both: revalidate() to fix the layout, then repaint() to render the result. Calling only one of them is the most common cause of "invisible component" and "stale display" bugs in Swing.

What revalidate() does

revalidate() marks a component as needing layout. It walks up the containment hierarchy to the nearest "validate root" (usually a JRootPane) and schedules a validate() call on the Event Dispatch Thread (EDT). During validation, the layout manager calls getPreferredSize(), getMinimumSize(), and getMaximumSize() on each child and assigns new bounds with setBounds().

java
1// After adding a component, revalidate triggers layout recalculation
2JPanel panel = new JPanel(new FlowLayout());
3JLabel label = new JLabel("New Label");
4panel.add(label);
5
6// Without this call, the label has zero bounds and won't appear
7panel.revalidate();

Internally, revalidate() calls RepaintManager.addInvalidComponent(this), which coalesces layout requests into a single pass. Calling revalidate() ten times in a row does not produce ten layout passes.

When to call revalidate()

  • After add() or remove() on a container
  • After changing a component's preferred, minimum, or maximum size
  • After changing a border that affects insets
  • After swapping a container's layout manager
  • After changing properties that affect a component's size (for example, the text of a JLabel that uses its preferred size)

What repaint() does

repaint() does not paint immediately. It posts a request to the EDT, and the RepaintManager coalesces requests into a single paint. For a JComponent, painting runs in this order:

  1. paintComponent(Graphics g) draws the component itself
  2. paintBorder(Graphics g) draws the border
  3. paintChildren(Graphics g) recursively paints child components
java
1// Custom painting example
2public class GradientPanel extends JPanel {
3    private Color startColor = Color.BLUE;
4
5    public void setStartColor(Color color) {
6        this.startColor = color;
7        repaint(); // Schedule visual update, no layout change needed
8    }
9
10    @Override
11    protected void paintComponent(Graphics g) {
12        super.paintComponent(g);
13        Graphics2D g2 = (Graphics2D) g;
14        g2.setPaint(new GradientPaint(0, 0, startColor, getWidth(), getHeight(), Color.WHITE));
15        g2.fillRect(0, 0, getWidth(), getHeight());
16    }
17}

When to call repaint()

  • After changing a visual property (color, font, icon) that does not affect layout
  • After updating custom painting state (animation frames, selection highlighting)
  • After revalidate() when components were structurally changed
  • When a model change should show up visually (for example, table data changed)

Why adding or removing components needs both

A change to the component hierarchy needs a new layout (revalidate()) and then a render of that layout (repaint()). With only revalidate(), stale pixels can stay on screen because the repaint region may not cover where the old components were. With only repaint(), components are drawn at their old positions and sizes.

java
1import javax.swing.*;
2import java.awt.*;
3
4public class DynamicPanel {
5    private int count = 0;
6    private final JPanel content = new JPanel(new FlowLayout());
7
8    public void createAndShowGUI() {
9        JFrame frame = new JFrame("revalidate + repaint Demo");
10        JButton addBtn = new JButton("Add Label");
11        JButton removeBtn = new JButton("Remove Last");
12
13        addBtn.addActionListener(e -> {
14            content.add(new JLabel("Item " + (++count)));
15            content.revalidate();  // Recalculate layout for the new label
16            content.repaint();     // Redraw the panel with updated layout
17        });
18
19        removeBtn.addActionListener(e -> {
20            if (content.getComponentCount() > 0) {
21                content.remove(content.getComponentCount() - 1);
22                content.revalidate();  // Recalculate layout without the removed label
23                content.repaint();     // Clear the area where the label was
24            }
25        });
26
27        JPanel buttons = new JPanel();
28        buttons.add(addBtn);
29        buttons.add(removeBtn);
30
31        frame.setLayout(new BorderLayout());
32        frame.add(new JScrollPane(content), BorderLayout.CENTER);
33        frame.add(buttons, BorderLayout.SOUTH);
34        frame.setSize(400, 300);
35        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
36        frame.setVisible(true);
37    }
38
39    public static void main(String[] args) {
40        SwingUtilities.invokeLater(() -> new DynamicPanel().createAndShowGUI());
41    }
42}

Side-by-side comparison

Aspectrevalidate()repaint()
PurposeRecalculate layout (sizes, positions)Redraw visual appearance (pixels)
TriggersComponent add/remove, size changesColor, font, icon, or painting state changes
Defined inJComponent (overrides Component.invalidate())Component
What it schedulesvalidate() on the nearest validate rootpaintComponent(), paintBorder(), paintChildren()
CoalescingYes, via RepaintManager.addInvalidComponent()Yes, via RepaintManager.addDirtyRegion()
Thread safetyMust be called on EDT (or it posts to EDT)Can be called from any thread (posts to EDT internally)
Effect without the otherLayout updates but old pixels may remainRedraws at old positions/sizes

How RepaintManager batches updates

Both methods go through javax.swing.RepaintManager, which batches UI updates. That is why Swing stays responsive even when code calls revalidate() and repaint() often.

java
1// RepaintManager coalesces updates automatically
2for (int i = 0; i < 100; i++) {
3    panel.add(new JLabel("Label " + i));
4}
5// One revalidate + repaint is sufficient for all 100 additions
6panel.revalidate();
7panel.repaint();
8
9// DO NOT call revalidate/repaint inside the loop - it works but wastes cycles
10// The RepaintManager would coalesce them anyway, but the overhead of
11// 100 addInvalidComponent calls is unnecessary

RepaintManager also handles double buffering. Swing paints to an off-screen buffer first and then copies it to the screen in one operation, which prevents the flickering that AWT's immediate-mode painting suffered from.

Calling them from the Event Dispatch Thread

Swing is single-threaded by design, so all UI changes must happen on the EDT.

java
1// WRONG: modifying UI from a background thread
2new Thread(() -> {
3    panel.add(new JLabel("From background"));
4    panel.revalidate();
5    panel.repaint();
6}).start();
7
8// CORRECT: use SwingUtilities.invokeLater
9new Thread(() -> {
10    // ... do heavy computation ...
11    SwingUtilities.invokeLater(() -> {
12        panel.add(new JLabel("From EDT"));
13        panel.revalidate();
14        panel.repaint();
15    });
16}).start();
17
18// ALSO CORRECT: use SwingWorker
19SwingWorker<List<String>, String> worker = new SwingWorker<>() {
20    @Override
21    protected List<String> doInBackground() {
22        // Background work here
23        return List.of("Result 1", "Result 2");
24    }
25
26    @Override
27    protected void done() {
28        // Runs on EDT automatically
29        try {
30            for (String item : get()) {
31                panel.add(new JLabel(item));
32            }
33            panel.revalidate();
34            panel.repaint();
35        } catch (Exception ex) {
36            ex.printStackTrace();
37        }
38    }
39};
40worker.execute();

repaint() is one of the few Swing methods that is safe to call from any thread, because it only posts a request to the EDT queue. revalidate() should still be called from the EDT.

Mistakes that cause ghost or missing components

  • Only revalidate() after adding components. The new components get correct bounds, but pixels from the previous layout may remain as ghost artifacts. Pair it with repaint().
  • Only repaint() after adding components. Components are painted at their old (or zero) bounds, so a new component is invisible or drawn at (0,0) with zero size.
  • Calling both inside a tight loop. RepaintManager coalesces the calls, but posting hundreds of requests is wasteful. Make all structural changes first, then call each method once.
  • Calling repaint() inside paintComponent(). This creates an infinite repaint loop that pegs the CPU at 100%. Use a javax.swing.Timer for animation.
  • Changing the UI off the EDT. This causes race conditions that show up as intermittent rendering glitches, exceptions, or deadlocks. Use SwingUtilities.invokeLater() or SwingWorker.
  • Skipping super.paintComponent(g) in custom painting. The background is not cleared, so old content bleeds through as smearing.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms