-1

I working on a program that prints 100 numbers in a jTable. Also, there will be an if statement to validate the results, and will set a jPanel in a Specific color according to the value that is printed. I need to print those values a little bit slower and also make sure that jPanel changes it colors according to each value. I tried the following code, but seems to have an error:

try{ 
  int n = 100;      
  int m = 1513;
  int a = 19713;
  double x = 177963;
  int c = 1397; 
  double r;     
  int i;

  Object[] res = new Object[n]; 

  for(i=0;i< n;i++){

    r = (a*x+c)%m;
    x = r;
    r = r/m;
    res[i] = r;
    Thread.sleep(1000);

    if(r>=0.3){
      jPanel3.setBackground(Color.green);                           
    }else{
      jPanel3.setBackground(Color.red);
    }
  }

  DefaultTableModel dtm = new DefaultTableModel();

  dtm.addColumn("Results", res);
  // dtm.addColumn("resultado2", res);
  jTable1.setModel(dtm);
}catch(Exception e){
  Thread.currentThread().interrupt();
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
E B
  • 5
  • 2
  • 5
  • 2
    Don't use `sleep`. user swing `Timer` – Paul Samsotha Nov 25 '13 at 02:00
  • 1
    possible duplicate of [Using Sleep method inside a For loop](http://stackoverflow.com/questions/20172831/using-sleep-method-inside-a-for-loop) – MadProgrammer Nov 25 '13 at 02:22
  • You're so lazy that not only did you learn nothing from your previous question and the fact that it was closed, you seem to have reposted it verbatim including the "(on hold)" in the title ?! – Boann Nov 25 '13 at 04:40

2 Answers2

3

but seems to have an error...

What error?

Note that you'll never want to call Thread.sleep(...) in a Swing application's event thread. Use a Swing Timer instead.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
2

Swing is a single threaded framework. The Event Dispatching Thread is responsible for processing, amongst other things, repaint requests. Any action which stops the EDT from running, will prevent it from processing any repaints requests and other events, making your application look like it's hung...

You are also required to ensure that all updates to the UI are made from within the context of the EDT.

In your case, you are executing a loop and using Thread.sleep, which are two big no, no's when dealing with Swing...

Take a look at Concurrency In Swing

As was mentioned in your duplicate question, you should be using a javax.swing.Timer.

This means you are going to have to revamp your loop conditions to work, for example...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ColorPane {

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

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

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

    public class TestPane extends JPanel {

        private Color[] colors = new Color[]{Color.RED, Color.GREEN, Color.BLUE};
        private int colorIndex = -1;

        public TestPane() {
            Timer timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    colorIndex++;
                    if (colorIndex >= colors.length) {
                        colorIndex = 0;
                    }
                    setBackground(colors[colorIndex]);
                }
            });
            timer.start();
        }

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

    }

}
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366