multithreading - Thread communication: How to signal that a key was clicked? java.lang.IllegalMonitorStateException -


i have simple javafx stage textfield. want is: when user inserts letters textfield, want print "now" (just if works). im using thread because later want scan dictonary see, if letters user entered part of words dictionary.

but get: java.lang.illegalmonitorstateexception

any ideas? don't seem understand whole concept of condition.await , multithreading..

import javafx.application.application; import javafx.scene.scene; import javafx.scene.control.textfield; import javafx.scene.layout.stackpane; import javafx.stage.stage; import java.util.concurrent.locks.condition; import java.util.concurrent.locks.lock; import java.util.concurrent.locks.reentrantlock;  public class dictionarythreading extends application {   private static lock lock = new reentrantlock(); public static condition condition = lock.newcondition();  public static void main(string[] args) {     launch(); }  private static class scanwords implements runnable{      @override     public void run() {         lock.lock();         try{                 while(true){                 this.wait();                 system.out.println("clicked");             }         } catch (exception e){             e.printstacktrace();         } finally{             lock.unlock();         }        }  }  @override public void start(stage primarystage) throws exception {     stackpane pane = new stackpane();      new scanwords().run();     textfield tf = new textfield("please enter word");     tf.setonkeypressed(e -> {});     pane.getchildren().add(tf);      scene scene = new scene(pane);     primarystage.setscene(scene);     primarystage.show();  }  } 

there no need create thread nothing other wait user events. javafx framework provides (it 1 of fundamental pieces of functionality of any ui toolkit). need respond changes in text in text field register change listener text field's text property:

public void start(stage primarystage) throws exception {     stackpane pane = new stackpane();      textfield tf = new textfield("please enter word");     tf.textproperty().addlistener((obs, oldtext, newtext) -> {         system.out.println("text changed");     });     pane.getchildren().add(tf);      scene scene = new scene(pane);     primarystage.setscene(scene);     primarystage.show();  } 

if thing need in response text changing takes long time, should launch process in background thread in listener on text field. if searching large, want cancel existing search, don't end large number of searches running concurrently. javafx service class provides functionality need this:

public class searchservice extends service<list<string>> {      // modify , access on fx application thread:     private string searchstring ;      @override     protected task<list<string>> createtask() {         final string s = searchstring ;         return new task<list<string>>() {             @override             protected list<string> call() throws exception {                 list<string> matches = new arraylist<>();                 // search strings matching s                 // sure check iscancelled() regularly                 return matches ;             }         };     }      public string getsearchstring() {         checkthread();         return searchstring ;     }      public void setsearchstring(string searchstring) {         checkthread();         this.searchstring = searchstring ;     }      private void checkthread() {         if (! platform.isfxapplicationthread()) {             throw new illegalstateexception("not on fx application thread");         }     } } 

then can do

public void start(stage primarystage) throws exception {     stackpane pane = new stackpane();      searchservice searchservice = new searchservice();      searchservice.setonsucceeded(e -> {         list<string> matches = searchservice.getvalue();         // whatever need search results...         // called on fx application thread     });      textfield tf = new textfield("please enter word");     tf.textproperty().addlistener((obs, oldtext, newtext) -> {         searchservice.cancel();         searchservice.setsearchtext(newtext);         searchservice.restart();     });     pane.getchildren().add(tf);      scene scene = new scene(pane);     primarystage.setscene(scene);     primarystage.show();  } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -