Recolor a component every second?

I would like to recolor the component after every second, but that did not work. I'm trying to:

    try{
        while(true){
            Thread.currentThread().sleep(1000);
            gc.cb.next();
            gc.repaint();
        }
    }
    catch(Exception ie){
    }
+3
source share
2 answers

I would advise using javax.swing.Timerfor this problem, which periodically triggers ActionEventevents in the thread sending (note that you should only rename and / or manipulate Swing components from this thread). You can then determine ActionListenerto intercept the event and repaint your component at that moment.

Example

JComponent myComponent = ...
int delay = 1000; //milliseconds

ActionListener taskPerformer = new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
    myComponent.repaint();
  }
};

new Timer(delay, taskPerformer).start();

, SwingWorker, , , , , .

+9

, UI- . UI-, repaint .

; sleep Thread.sleep(...). ( thatThread.sleep(...) .)

"" , , SwingWorker. .

, .

+1

Source: https://habr.com/ru/post/1748490/


All Articles