repaint() in Java [duplicate]

If you added JComponent to already visible Container, then you have call frame.getContentPane().validate(); frame.getContentPane().repaint(); for example import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(460, 500); frame.setTitle(“Circles generator”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); SwingUtilities.invokeLater(new Runnable() { public void run() { … Read more

JLayeredPane and painting

As you found, a BufferedImage is an effective way to cache complex content for efficient rendering; CellTest is an example. A flyweight renderer, shown here, is another approach. Finally, I’ve re-factored your instructive example in a way that may make experimentation easier. import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import … Read more

repaint in a loop

You can use JComponent.paintImmediately to force an immediate repaint. EDIT: After reading your question again it occurs to me that you might be executing your logic on the event dispatch thread. This would mean that the repaint requests will not be executed until after your method returns. If you put your code into another thread … Read more

How to improve painting performance of DataGridView?

I recently had some slowness issues with DataGridView and the solution was the following code public static void DoubleBuffered(this DataGridView dgv, bool setting) { Type dgvType = dgv.GetType(); PropertyInfo pi = dgvType.GetProperty(“DoubleBuffered”, BindingFlags.Instance | BindingFlags.NonPublic); pi.SetValue(dgv, setting, null); } It turns double buffering on for DataGridView objects. Just call DoubleBuffered() on your DGV. Hope it … Read more

Is the Swing repaint() method still safe to use outside the EDT in Java 7+?

This is the official reference: Swing’s Threading Policy In general Swing is not thread safe. All Swing components and related classes, unless otherwise documented, must be accessed on the event dispatching thread. And the repaint method does not “document otherwise”. To doubly reassure you that you do not need to look any further than an … Read more

What’s the difference between reflow and repaint?

This posting seems to cover the reflow vs repaint performance issues http://www.stubbornella.org/content/2009/03/27/reflows-repaints-css-performance-making-your-javascript-slow/ As for definitions, from that post: A repaint occurs when changes are made to an elements skin that changes visibly, but do not affect its layout. Examples of this include outline, visibility, background, or color. According to Opera, repaint is expensive because the … Read more