Sunday, 15 August 2010

java - Swing keeps spawning thread on invokeLater() -



java - Swing keeps spawning thread on invokeLater() -

i'm new java ui , swing , can't understand why happening.

public class zasciimapwindow extends jframe implements keylistener, runnable { ... // swing stuff private jtextarea displayarea = null; private jtextfield typingarea = null; public zasciimapwindow(final zmap map, final zhuman player) { super("zasciimapwindow"); this.map = map; this.player = player; } ... public void show() { seek { uimanager.setlookandfeel("javax.swing.plaf.metal.metallookandfeel"); } grab (unsupportedlookandfeelexception ex) { ex.printstacktrace(); } grab (illegalaccessexception ex) { ex.printstacktrace(); } grab (instantiationexception ex) { ex.printstacktrace(); } grab (classnotfoundexception ex) { ex.printstacktrace(); } /* turn off metal's utilize of bold fonts */ uimanager.put("swing.boldmetal", boolean.false); //schedule job event dispatch thread: //creating , showing application's gui. javax.swing.swingutilities.invokelater(this); } private void addcomponentstopane() { this.typingarea = new jtextfield(20); this.typingarea.addkeylistener(this); this.typingarea.setfocustraversalkeysenabled(false); this.displayarea = new jtextarea(); this.displayarea.seteditable(false); jscrollpane scrollpane = new jscrollpane(this.displayarea); scrollpane.setpreferredsize(new dimension(375, 125)); getcontentpane().add(this.typingarea, borderlayout.page_start); getcontentpane().add(scrollpane, borderlayout.center); } /** * create gui , show it. thread safety, * method should invoked * event-dispatching thread. */ private void createandshowgui() { //create , set window. this.setdefaultcloseoperation(jframe.exit_on_close); //set content pane. this.addcomponentstopane(); //display window. this.pack(); this.setvisible(true); } @override public void run() { createandshowgui(); } }

then when phone call new zasciimapwindow(x, y).show() main(), never shows jframe. , if debug find out keeps calling createandshowgui() infinite.

why happening? in advance.

javax.swing.swingutilities.invokelater(this); calls run method of passed runnable. run method createandshowgui();, calls this.setvisible(true); assume calls this.show() calls javax.swing.swingutilities.invokelater(this);.

so behaviour not surprising.

i start avoiding having class extend jframe, implement keylistener , runnable.

for example, practice have jframe within class instead of extending jframe directly.

java swing

No comments:

Post a Comment