java - JButton text not changing -
so code has jpanel
text fields , jbutton
, , when user clicks button goes button listener, takes data text fields , processes it, creating jlabels
adds invisible jpanel
. make first jpanel invisible, , make second panel visible, revealing "results" generate.
this works problem is, while program processes data gets text fields, want jbutton
change says, , tried using event.getsource().settext()
, , able find changing button text (by printing console), not updating button changed text.
i tried forms of revalidate , repaint , validate after this, none of worked. ideas? thanks!
//entrypanel first panel, , pickspanel second panel button.addactionlistener(new actionlistener() { public void actionperformed(actionevent event) { ((jbutton)event.getsource()).settext("thinking..."); revalidate(); repaint(); try { criticpick picks = new criticpick(cityfield.gettext(),statefield.gettext()); linkedlist<movie> pickslist = picks.getlist(); glayout.setrows(pickslist.size()+2+thepicks.movnum); pickspanel.add(new jlabel("the results:")); //in actual code bunch of processing , looping results in jlabels being added pickspanel (int i=0;i<pickslist.size();i++) { jlabel label = new jlabel(pickslist.get(i).title); pickspanel.add(label); } } catch (exception exc) { system.out.println(exc); } entrypanel.setvisible(false); pickspanel.setvisible(true); }}); guiframe.add(entrypanel); guiframe.add(pickspanel); guiframe.setlayout(new flowlayout(flowlayout.left)); guiframe.setvisible(true); }
the reason no paint event processed before job completed, effect of settext
won't show when think will.
repaint()
promises repaint "as possible" (per docs), since paint events processed on same thread button click events (the "event dispatch thread"), processing causes settext
call , panel hiding/showing occur @ same time (so speak), after job done.
the effect of blocking edt data crunching jobs obvious if job takes noticeable amount of time - ui become unresponsive since edt doesn't have opportunity repaint, process resize/mouse click events, etc.
heavy processing should done in background threads, preferrably aid of swingworker
. leaves edt free handle paint (and other) events.
Comments
Post a Comment