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().
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()orremove()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:
paintComponent(Graphics g)draws the component itselfpaintBorder(Graphics g)draws the borderpaintChildren(Graphics g)recursively paints child components
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.
Side-by-side comparison
| Aspect | revalidate() | repaint() |
| Purpose | Recalculate layout (sizes, positions) | Redraw visual appearance (pixels) |
| Triggers | Component add/remove, size changes | Color, font, icon, or painting state changes |
| Defined in | JComponent (overrides Component.invalidate()) | Component |
| What it schedules | validate() on the nearest validate root | paintComponent(), paintBorder(), paintChildren() |
| Coalescing | Yes, via RepaintManager.addInvalidComponent() | Yes, via RepaintManager.addDirtyRegion() |
| Thread safety | Must be called on EDT (or it posts to EDT) | Can be called from any thread (posts to EDT internally) |
| Effect without the other | Layout updates but old pixels may remain | Redraws 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.
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.
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 withrepaint(). - 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.
RepaintManagercoalesces the calls, but posting hundreds of requests is wasteful. Make all structural changes first, then call each method once. - Calling
repaint()insidepaintComponent(). This creates an infinite repaint loop that pegs the CPU at 100%. Use ajavax.swing.Timerfor animation. - Changing the UI off the EDT. This causes race conditions that show up as intermittent rendering glitches, exceptions, or deadlocks. Use
SwingUtilities.invokeLater()orSwingWorker. - Skipping
super.paintComponent(g)in custom painting. The background is not cleared, so old content bleeds through as smearing.
Related reading
- Java switch statement Constant expression required, but it IS constant
- Java Synchronized Block for .class
- Java synchronized method
- Java synchronized method lock on object, or method?
- Java system properties and environment variables
- Java System.currentTimeMillis equivalent in C
- Java text classification problem
- Java thread executing remainder operation in a loop blocks all other threads
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.