java - Thread.sleep() crashes my GUI -


my thread.sleep(rand.nextint(delay)) command in buttonlistener class crashes gui. ideas? program supposed add people arraylist, randomly select them , display them @ random time between 0 , timetext jtextfield, , works until add sleep command. appreciated thanks!

import javax.swing.*; import java.awt.event.*; import java.util.*; import java.awt.*; import javax.swing.timer;  public class myprogram extends appclass{   protected int x,y,width,height;   protected color color;   private arraylist<string> people = new arraylist<string>();    private static jlabel person;   private timer timer;   private buttonlistener listener;   private random rand = new random();   private jlabel addpeople;   private jtextfield newperson;   private jtextfield timetext;    private font font1 = new font("arial",1,17);   private font font2 = new font("arial",1,65);    public myprogram(){     setpreferredsize(new dimension(1000,800));      people.add("me");     people.add("john");     people.add("greg");      jpanel panel = new jpanel();     panel.setpreferredsize(new dimension(600,400));     panel.setlayout(new boxlayout(panel, boxlayout.y_axis));      newperson = new jtextfield(2);     newperson.setfont(font1);     addpeople = new jlabel("add people:");     addpeople.setfont(font1);     person = new jlabel();     person.setfont(font2);     jbutton addbutton = new jbutton("add");     addbutton.setfont(font1);     jbutton startbutton = new jbutton("start");     startbutton.setfont(font1);     timetext = new jtextfield(2);     timetext.setfont(font1);     jlabel time = new jlabel("maximum time between draws:");     time.setfont(font1);      listener = new buttonlistener();     addbutton.addactionlistener(listener);     startbutton.addactionlistener(listener);      panel.add(addpeople);     panel.add(newperson);     panel.add(addbutton);     panel.add(time);     panel.add(timetext);     panel.add(startbutton);     panel.add(person);      add(panel);   }    private class buttonlistener implements actionlistener{      public void actionperformed(actionevent ae){       jbutton button = (jbutton) ae.getsource();        if(button.gettext().equals("add")){         people.add(newperson.gettext());         system.out.println(newperson.gettext());         system.out.println("also worked");        }else if(button.gettext().equals("start")){         int delay = integer.parseint(timetext.gettext());         for(;;){         person.settext(people.get(rand.nextint(people.size())));         try{         thread.sleep(rand.nextint(delay)); // problem       }catch(exception error){         system.out.println("error");       }           }       }     }   }   } import javax.swing.*; import java.awt.event.*; import java.util.*; import java.awt.*; import javax.swing.timer;  public class appclass extends jpanel{    public static void main(string [] args){     jframe frame = new jframe();     frame.getcontentpane().add(new get());     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.pack();     frame.setvisible(true);     frame.settitle("my program");     } } 

there 1 thread modifies ui. design, because means that, many sources of events ui receive, impossible concurrency bugs corrupt ui state.

there 1 thread modifies ui. includes:

  • receiving mouse events.
  • receiving keyboard events.
  • processing requests repaint.
  • processing ui timers.

among many others.

if you're in code modifies ui, must on ui thread (else, have bug). if you're on ui thread , call sleep(), ui thread stops doing things.

it'll stop responding requests repaint. it'll stop responding keyboard events, mouse events, etc.

instead, have use forms timer perform animation. when clicks "start" button, set first value, save off rest of values, , start timer, , let ui thread keep processing.

each time timer fires, advance state - update ui next value want show. continue until you've shown values, , stop timer , release state told in animation.

yeah, you're setting text on component, still falls under animation pattern.

be careful though - if ui closed while animation timer still running, you'll try modify ui gone. ui code has careful stop animation timer if it's still running when ui closed.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -