java - SwingUtilities.invokeLater, SwingWorker and gif icon animation issue -
i trying load gif icon, while doing expensive work in background. issue this, gif(animation) icon not loading.
if replace gif icon png, works fine.
here test class code
import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.net.url; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; public class test { public static void main(string[] args) { // todo auto-generated method stub jframe frame = new jframe(); frame.setbounds(100, 100, 200, 100); frame.setdefaultcloseoperation(jframe.exit_on_close); jpanel panel = new jpanel(); frame.getcontentpane().add(panel); jbutton loginbutton = new jbutton("login"); panel.add(loginbutton); frame.add(panel); frame.setvisible(true); loginbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { // todo auto-generated method stub url urlprocessing = test.class.getresource("processing.gif"); //url urlprocessing = test.class.getresource("processing.png"); imageicon processingicon = new imageicon(urlprocessing); loginbutton.settext("authenticating..."); loginbutton.seticon(processingicon); loginbutton.revalidate(); loginbutton.repaint(); swingutilities.invokelater(new runnable() { @override public void run() { worker worker = new worker(frame); try { worker.doinbackground(); } catch (exception e1) { e1.printstacktrace(); } } }); } }); } } when user click login button, change text of button , put gif icon on it. in
swingutilities.invokelater(new runnable() {
method start background expensive task through swingworker thread. right now, no need publish , process methods of swingworker class, because not doing work during background expensive task.
if remove "swingutilities.invokelater(new runnable() {" method , call directly swingworker class, both gif , png icons not working(displaying on button).
i tried simple thread change text , set icon on button, did not worked well.
here swingworker class code
import javax.swing.jframe; import javax.swing.swingworker; public class worker extends swingworker<string, string> implements runnable { private jframe testframe; // public , private properties public worker(jframe frame){ // doing assignments this.testframe = frame; } @override protected void done() { // todo auto-generated method stub super.done(); system.out.println("done method called"); } @override protected string doinbackground() throws exception { int = 0; string chunks = null; publish(chunks); // doing expensive work, http request, db operations etc... while (i < 10) { thread.sleep(1000); system.out.println("value of i: "+i); i++; } this.done(); testframe.setvisible(false); return null; } } any idea? happening in gui main thread? mistake?
you shouldn't call doinbackground directly, should call execute instead. should able make call in ui thread (i.e. no need call inside invokelater). in doinbackground implementation, should changes ui in ui thread (i.e. invokelater should used there).
Comments
Post a Comment