Updating main GUI Elements in Java Swing -
i'm trying update main gui in java swing application, there runnable thread keeps main gui visible, problem is called in main, , main static function. element.setttext. calls want update not static. how can update lables,..etc in main gui then?
public static void main(string args[]) { java.awt.eventqueue.invokelater(new runnable() { public void run() { new agc().setvisible(true); // code update labels here } }); }
what understood question think static means non-changeable. not true java. in java objects , components never change characterized final.
keep main simple , small , make loops , changes in dothings();
here timer in order update text of jlabel:
import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.timer; public class foo extends jframe { public foo() { jlabel1 = new jlabel("label 1"); jpanel1 = new jpanel(); jpanel1.add(jlabel1); add(jpanel1); pack(); // code update whatever here dothings(); } private void dothings() { // code update whatever here actionlistener actionlistener = new actionlistener() { @override public void actionperformed(actionevent actionevent) { jlabel1.settext("foo " + (j++)); } }; timer timer = new timer(500, actionlistener); timer.start(); } public static void main(string args[]) { java.awt.eventqueue.invokelater(new runnable() { @override public void run() { new foo().setvisible(true); } }); } private jlabel jlabel1; private jpanel jpanel1; private int j = 0; }
Comments
Post a Comment