java - Why does calling this method on the EDT cause a compilation error? -
i trying pop custom dialog box. when try calling method on edt following error:
exception in thread "awt-eventqueue-0" java.lang.error: unresolved compilation problem: @ danind.com.gmail_coem.ui.credentialeditor.promptpossibledialog(credentialeditor.java:29) @ danind.com.gmail_coem.ui.homescreen$configuredatabase.<init>(homescreen.java:281) @ danind.com.gmail_coem.ui.homescreen.configuredatabase(homescreen.java:230) @ danind.com.gmail_coem.ui.homescreen.lambda$1(homescreen.java:105) @ danind.com.gmail_coem.ui.homescreen$$lambda$7/2092062410.actionperformed(unknown source) @ javax.swing.abstractbutton.fireactionperformed(unknown source) @ javax.swing.abstractbutton$handler.actionperformed(unknown source) @ javax.swing.defaultbuttonmodel.fireactionperformed(unknown source) @ javax.swing.defaultbuttonmodel.setpressed(unknown source) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(unknown source) @ java.awt.awteventmulticaster.mousereleased(unknown source) @ java.awt.awteventmulticaster.mousereleased(unknown source) @ java.awt.component.processmouseevent(unknown source) @ javax.swing.jcomponent.processmouseevent(unknown source) @ java.awt.component.processevent(unknown source) @ java.awt.container.processevent(unknown source) @ java.awt.component.dispatcheventimpl(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.lightweightdispatcher.retargetmouseevent(unknown source) @ java.awt.lightweightdispatcher.processmouseevent(unknown source) @ java.awt.lightweightdispatcher.dispatchevent(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.window.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$400(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)
after cleaning project in eclipse, , doing isolation tests figured out calling method on edt caused problem. when moved method background thread worked, don't want since want create dialog gui on edt.
//creates compilation error private class configuredatabase extends swingworker<void, string[]> { private credentialeditor instance; public configuredatabase() { //runs on edt this.instance = credentialeditor.promptpossibledialog(true); } @override protected void doinbackground() { //runs in background thread try(database database = credentialeditor.getcredentials(instance)) { //code } } }
vs
//runs fine, dialog gui not on edt private class configuredatabase extends swingworker<void, string[]> { @override protected void doinbackground() { //runs in background thread try(database database = credentialeditor.getcredentials(credentialeditor.promptpossibledialog(true))) { //code } } }
the method in question:
public static credentialeditor promptpossibledialog(boolean reset) { if(reset || connectionpool.getinstance() == null) { //checks see if dialog box needs created. if(swingutilities.iseventdispatchthread()) { //checks make sure thread on edt. return new credentialeditor(); } else { //if it's not on edt throw exception warning. throw new illegalstateexception("must run on edt!"); } } return null; //if no dialog box needs created return nothing. }
to more detailed problem seems calling method causes problems. it's not setting instance variable or inside method, it's calling static method in edt specifically. in fact, stacktrace points line it's stating method, in, line says public static credentialeditor promptpossibledialog(boolean reset)
so causing error , if can't around how can run gui code on edt if method being called on background thread?
try running dialog directly on edt.
public configuredatabase() { //some code this.instance = credentialeditor.promptpossibledialog(true); //this line 281 }
this means running dialog in worker thread, not idea. worker threads mostly, afaik, non interactive background tasks. if must run dialog within worker thread, must separately start of in edt like:
public configuredatabase() { swingutilities.invokelater(new runnable() { //or if must wait end, use invokeandwait public void run() { credentialeditor.promptpossibledialog(true); //this line 281 } }); }
this should work. more helpful if provide full ssce, @ first how execute worker.
also dialogs interacting user , bringing result. saving dialog in instance therefor not best thing do. instead store result or rethink design.
see here, example:
note: calling on event dispatch thread blocks events, including repaints, being processed until swingworker complete.
when want swingworker block on event dispatch thread recommend use modal dialog.
for example:
class swingworkercompletionwaiter extends propertychangelistener { private jdialog dialog; public swingworkercompletionwaiter(jdialog dialog) { this.dialog = dialog; } public void propertychange(propertychangeevent event) { if ("state".equals(event.getpropertyname()) && swingworker.statevalue.done == event.getnewvalue()) { dialog.setvisible(false); dialog.dispose(); } } }
run as:
jdialog dialog = new jdialog(owner, true); swingworker.addpropertychangelistener( new swingworkercompletionwaiter(dialog)); swingworker.execute(); //the dialog visible until swingworker done dialog.setvisible(true);
specified by: in interface future returns: computed result throws: interruptedexception - if current thread interrupted while waiting executionexception - if computation threw exception
Comments
Post a Comment