multithreading - How to initialize gui objects in a thread safe manner in java swing? -



multithreading - How to initialize gui objects in a thread safe manner in java swing? -

i'm reading thinking in java , author stresses main method shouldn't phone call swing methods. illustration of practice presents next piece of code (available on webpage):

//: gui/submitswingprogram.java import javax.swing.*; import java.util.concurrent.*; public class submitswingprogram extends jframe { jlabel label; public submitswingprogram() { super("hello swing"); label = new jlabel("a label"); add(label); setdefaultcloseoperation(jframe.exit_on_close); setsize(300, 100); setvisible(true); } static submitswingprogram ssp; public static void main(string[] args) throws exception { swingutilities.invokelater(new runnable() { public void run() { ssp = new submitswingprogram(); } }); timeunit.seconds.sleep(1); swingutilities.invokelater(new runnable() { public void run() { ssp.label.settext("hey! different!"); } }); } } ///:~

the gui object created , initialized through invokelater method making thread safe. few pages later author presents next code:

//: gui/button2.java // responding button presses. import javax.swing.*; import java.awt.*; import java.awt.event.*; import static net.mindview.util.swingconsole.*; public class button2 extends jframe { private jbutton b1 = new jbutton("button 1"), b2 = new jbutton("button 2"); private jtextfield txt = new jtextfield(10); class buttonlistener implements actionlistener { public void actionperformed(actionevent e) { string name = ((jbutton)e.getsource()).gettext(); txt.settext(name); } } private buttonlistener bl = new buttonlistener(); public button2() { b1.addactionlistener(bl); b2.addactionlistener(bl); setlayout(new flowlayout()); add(b1); add(b2); add(txt); } public static void main(string[] args) { run(new button2(), 200, 150); } } ///:~

where swingconsole is:

//: net/mindview/util/swingconsole.java // tool running swing demos // console, both applets , jframes. bundle net.mindview.util; import javax.swing.*; public class swingconsole { public static void run(final jframe f, final int width, final int height) { swingutilities.invokelater(new runnable() { public void run() { f.settitle(f.getclass().getsimplename()); f.setdefaultcloseoperation(jframe.exit_on_close); f.setsize(width, height); f.setvisible(true); } }); } } ///:~

so contrary previous illustration object implementing jframe created , initialized within main method / main thread.

my question then: (1) sec illustration wrong or first 1 exaggerated? (2) plenty phone call swing methods through invokelater after setvisible phone call , before statement safe phone call swing methods within main thread?

the sec illustration wrong. swing components must created , used event dispatch thread. see https://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html.

quote the javadoc:

calls application's main method, or methods in applet, not invoked on event dispatching thread. such, care must taken transfer command event dispatching thread when constructing , showing application or applet.

(emphasis mine)

java multithreading swing thread-safety event-dispatch-thread

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -