Question

Is it possible to repaint a JPanel from within a loop in another object? I have a JFrame that consists of a JPanel (DrawPanel) and a SA object. I would like to update/repaint the JPanel during the while loop in this SA object. I started a new thread, but still panel.repaint() does not execute.

public class Mainform extends JFrame {
    private DrawPanel DrawPanel;
    public static void main(String[] args) {
        DrawPanel panel = new DrawPanel();
        SA sa = new SA(panel);
        Thread t = new Thread(sa);
        t.start();
        //...
    }
}
public class DrawPanel extends JPanel implements MouseMotionListener, MouseListener {
    public DrawPanel() {
        super();
        setBackground(Color.WHITE);
        addMouseWheelListener(this);
        addMouseListener(this);
        addMouseMotionListener(this);
    }
    //...
}
public class SA implements Runnable {
    private DrawPanel panel;
    public SA(DrawPanel p) {
        this.panel = p;
        init();
    }
    public void run() {
        while (true) {
            //...
            panel.repaint();
        }
    }
}

EDIT: run is public

Was it helpful?

Solution

The basic answer is "yes".

This assumes that the component you are trying to repaint is

  1. Added to a container
  2. That container is attached to some kind of native peer (ie a window)
  3. That window is visible.

The RepaintManager is generally smart enough to know not to waste time painting something that isn't displayable.

The following example is rather basic, but will increment a counter within the paintComponent of a JPanel each time it is called. The Runnable, which is attached to a Thread, will update every second...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RepaintTest {

    public static void main(String[] args) {
        new RepaintTest();
    }

    public RepaintTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                TestPane tp = new TestPane();
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(tp);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                Thread thread = new Thread(new Repainter(tp));
                thread.setDaemon(true);
                thread.start();
            }
        });
    }

    public class Repainter implements Runnable {

        private JPanel panel;

        public Repainter(JPanel panel) {
            this.panel = panel;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                }
                panel.repaint();
            }
        }

    }

    public class TestPane extends JPanel {

        private int repaints = 0;

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics g2d = (Graphics2D) g.create();
            repaints++;
            FontMetrics fm = g2d.getFontMetrics();
            String text = Integer.toString(repaints);
            int x = (getWidth() - fm.stringWidth(text)) / 2;
            int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
            g2d.drawString(text, x, y);
            g2d.dispose();
        }

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top