1 2 3 Previous Next 30 Replies Latest reply on Dec 29, 2010 3:34 AM by darrylburke Go to original post
      • 16. Re: force the repaint of a JPanel
        Nice. You're my hero. That one works, nothing else I found worked. Awesome. Props. You have my gratitude. A+. I'd offer you a job, but I don't own a company.
        • 17. Re: force the repaint of a JPanel

          It didn't quote. I was replying to:

          • 18. Re: force the repaint of a JPanel
            I was having a similar problem with a JPanel where I needed to dynamically add or take away labels. The only way I could get it to repaint was to resize the window or do some action which caused a JScrollPane in another panel to turn on or off by adding and removing text.

            I tried every suggestion mentioned here, revalidate() did the trick.

            • 19. Re: force the repaint of a JPanel

              i'm facing quite the same problem
                   public void HighlightCell(boolean highlight)
                        Graphics g = getGraphics();
                        if (g != null) paintComponent(g);
                        else repaint();
                   public void paintComponent(Graphics grp)
                        Graphics2D g2d = (Graphics2D) grp;
                        // draw the image using the AffineTransform
                        if (m_highlight)
                        g2d.drawImage(m_img, m_transformer, this);     
              HighlightCell is called from an actionButton click. From there i would like to repaint the cell in XORMode. But calling repaint() does not do the trick neither update revalidate or whatever...
              But if i click on maximise or iconify the chage operates...
              What should i do ? Thanks for your help.
              • 20. Re: force the repaint of a JPanel
                so start your own thread
                • 21. Re: force the repaint of a JPanel
                  Literally - the solution is to instantiate a new Thread, put the processing that you need to do in the run method, and set it running when you would have otherwise started the processing that you need to do. This is the best solution I have found. This frees up the GUI to resume the repaint in the background while your processing continues.
                  • 22. Re: force the repaint of a JPanel
                    I had the same problem, and found an example of how to fix it (by putting the animation code in another string) at http://www.developer.com/java/other/article.php/158709, by a professor named Dick Baldwin (His tutorials can be found at http://dickbaldwin.com/, and are great resources)

                    I took a class from Prof. Baldwin, but I have no commercial association with him nor commercial interest in his tutorials (So I hope that this posting does not violate the code of conduct)
                    • 23. Re: force the repaint of a JPanel
                      this has solved same problem on my project too!
                      thanx bxpeng.
                      • 24. Re: force the repaint of a JPanel
                        This problem actually, from my experience, calls for using 2 Threads that will switch strictly.
                        In the first thread, you call a repaint every time you have data ready for displaing. In the other thread, you do any computations necessary. Also, you do the sleeping in this thread.
                        I think that you can learn best from an example, so I give you some animation code that should work 100% (I tested it before posting).
                        //contents of JavaDraw.java:
                        package javadraw;
                        import java.awt.Color;
                        import java.awt.Dimension;
                        import java.awt.Graphics;
                        import java.awt.Toolkit;
                        import javax.swing.JWindow;
                        public class AnimationWindow extends JWindow{
                            // this variable is used for strict switching between the threads
                            public static boolean computing = false;
                            // this is how fast the animation will be (one change every 10 ms)
                            public static int delay = 10;
                            // gray is iterated every computation cycle
                            static int gray = 0;
                            public static void main(String[] args) {
                                //some initialization stuff
                                AnimationWindow w = new AnimationWindow();
                                // the threads switch strictly - first the ComputeThread, then AnimationWindow
                                // and again the ComputeThread ...
                                new ComputeThread(w);
                            // just to have the animation in the center of the screen;
                            // this method is not necessary for the correct functionality of the animation
                            private static void centerOnScreen(JWindow w) {
                                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                            // This method belongs to the AnimationWindow object. 
                            // It is synchronized - i.e. no other user thread can call a method on the same 
                            // object while this method is being executed.
                            // It must be synchronized in order for the Thread switching to work correctly.
                            synchronized public void compute(){
                                            try {
                                            } catch (InterruptedException ex) {
                                try {
                                    // HERE you set the first thread to sleep, while the other one can draw freely.
                                    // Note that if you set the delay to something close to (or) zero, the animation
                                    // won't be smooth anymore, because JVM won't have enough time for a redraw.
                                    } catch (InterruptedException ex) {
                                //compute something... (this part is, of course, not necessary)
                                for(int i = 0; i<10000;i++){
                                    for(int j = 0;j<10000;j++) ; //some CPU-intensive computation
                                //set the next color to draw
                                if(gray+1 >= 256) System.exit(0);
                                AnimationWindow.computing = false;
                            // method whose only purpose is to call repaint() every time new data is computed
                            synchronized public void draw(){
                                           try {
                                            } catch (InterruptedException ex) {
                                AnimationWindow.computing = true;
                            // the paint method that is scheduled for calling after you call a repaint();
                            public void paint(Graphics g){
                                g.setColor(new Color(gray,gray,gray));
                        // The thread that calls any CPU-intensive computations
                        // It also prepares data for drawing.
                        class ComputeThread extends Thread{
                            AnimationWindow syncObject;
                            public ComputeThread(AnimationWindow w){
                                syncObject = w;
                                start(); //start() dispatches the ComputeThread and calls its run() method.
                            public void run(){
                        • 25. Re: force the repaint of a JPanel
                          Your approach to animation is slightly flawed.
                          What I advise is a game loop or some sort of program timer that schedules repaints every so many ms.

                          1) Create a timer object and use the method scheduleAtFixedRate.
                          2) Create a thread that calls an animation update and the repaint (same as #1 but explicit)

                          Good luck! :)
                          • 26. Re: force the repaint of a JPanel
                            Use something like component.paintImmediately(0, 0, component.getWidth(), component.getHeight())

                            Maybe the JVM has changed since the earlier posts, but repaint() does NOT cause the component to be repainted as soon as possible, contrary to the JavaDoc. It may actually allow a time delay to wait for similar events, in order to improve overall efficiency.

                            For example, if you have a regular event with a repaint() every 500 or 100 ms, the RepaintManager (?) may repaint each time at first, but then it adapts to this regular pattern and waits for all the calls to finish before painting anything. The source of this information is actual experimentation.

                            See also the Note under "Paint Processing" at http://java.sun.com/products/jfc/tsc/articles/painting/
                            • 27. Re: force the repaint of a JPanel
                              Actually, you have at least a couple problems that I can see in just a quick glance.

                              1 - paintComponent (is used in SWING)
                              2 - loading with Toolkit has never been advocated by anyone for anything other than tiny images. If you're image is bigger than a thumbnail, then you're not getting it loaded. Use ImageIO to load it.

                              I have literally, have 1000's of object running full screen in SWNG using repaint() and not have a problem with the graphics refreshing, and lest you think it was on a mega box, it was on anything from a 1.8GHz laptop to a dual quadcore. If you do it right, it will work right.
                              • 28. Re: force the repaint of a JPanel
                                If you absolutely have to have paints done exactly when you decide, then get rid of your passive approach and use active rendering.
                                • 29. Re: force the repaint of a JPanel
                                  edit: nvm, this thread is too old.