2012-12-12 101 views
2

我正在使用JavaFX創建通用聊天應用程序。我創建使用SceneBuilder一個ListView與ObservableList仿照它象下面這樣:javaFX中的observablelist更新問題

public static ObservableList<String> members = FXCollections.observableArrayList(); 

@FXML public static Button send; 
@FXML static ListView names; 
@FXML static HTMLEditor outmsg; 
@FXML static HTMLEditor showBox; 

static void updateRemove(String newuser) { 

// showBox.setHtmlText(showBox.getHtmlText()+newuser+"<br> has left room"); 
    System.out.println(newuser+"has left room"); 
    members.remove(newuser); 
    System.out.println("helloji update remove"); 


} 
@FXML public void sendAction(ActionEvent event) 
{ 


LoginController.instance.c.sendMessage(outmsg.getHtmlText()); 


} 
public static void updateList(String name) 
{ 
    // showBox.setHtmlText(showBox.getHtmlText()+"<br> new user entered in room"); 
    System.out.println("new user enterd"+name); 
    System.out.println("beginning update list"); 
    members.add(name); 
    System.out.println(members); 
    System.out.println("add user request fullfilled"); 


} 
public static void initList(Vector<String> name) 
{ 
    System.out.println("list initializing"); 
    members.setAll(name); 
     System.out.println(members); 
    System.out.println("list initialized"); 

    System.out.println("public room created"); 


} 
public static void showMessage(String msg) 
{ 

    // showBox.setHtmlText(showBox.getHtmlText()+""+msg); 
     System.out.println("showing msg "+msg); 
} 

@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 



    names.setItems(members); 
    // showBox.setHtmlText(showBox.getHtmlText()+"<br> welcome "+Client.username); 
     System.out.println("welcome "+Client.username); 

    System.out.println(names.getItems()); 

}  

}

但是當其updateRemove()方法被調用時,拋出異常:

java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4 
    at com.sun.javafx.tk.Toolkit.checkFxUserThread(Unknown Source) 
    at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Unknown Source) 
    at javafx.scene.Parent$1.onProposedChange(Unknown Source) 
    at com.sun.javafx.collections.VetoableObservableList.setAll(Unknown Source) 
    at com.sun.javafx.collections.ObservableListWrapper.setAll(Unknown Source) 
    at com.sun.javafx.scene.control.skin.LabeledSkinBase.updateChildren(Unknown Source) 
    at com.sun.javafx.scene.control.skin.LabeledSkinBase.handleControlPropertyChanged(Unknown Source) 
    at com.sun.javafx.scene.control.skin.SkinBase$4.changed(Unknown Source) 
    at javafx.beans.value.WeakChangeListener.changed(Unknown Source) 
    at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(Unknown Source) 
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown Source) 
    at javafx.beans.property.StringPropertyBase.fireValueChangedEvent(Unknown Source) 
    at javafx.beans.property.StringPropertyBase.markInvalid(Unknown Source) 
    at javafx.beans.property.StringPropertyBase.set(Unknown Source) 
    at javafx.beans.property.StringPropertyBase.set(Unknown Source) 
    at javafx.beans.property.StringProperty.setValue(Unknown Source) 
    at javafx.scene.control.Labeled.setText(Unknown Source) 
    at com.sun.javafx.scene.control.skin.ListViewSkin$12.updateItem(Unknown Source) 
    at javafx.scene.control.ListCell.updateItem(Unknown Source) 
    at javafx.scene.control.ListCell.access$000(Unknown Source) 
    at javafx.scene.control.ListCell$5.onChanged(Unknown Source) 
    at com.sun.javafx.scene.control.WeakListChangeListener.onChanged(Unknown Source) 
    at com.sun.javafx.collections.ObservableListWrapper.callObservers(Unknown Source) 
    at com.sun.javafx.collections.ObservableListWrapper.remove(Unknown Source) 
    at com.sun.javafx.collections.ObservableListWrapper.remove(Unknown Source) 
    at truechatter.RoomController.updateRemove(RoomController.java:45) 
    at truechatter.Client.run(Client.java:78) 

initList()法正常工作。爲什麼?

+0

請標記答案爲已接受 –

回答

3

UI只能由gui-thread更新。

用途:

// create JavaFX scene 
Platform.runLater(new Runnable() { 
    public void run() { 
     members.add(name); 
    } 
}); 

爲什麼初始化()方法的工作,可能是因爲它已經從FX應用程序線程調用

從javadoc中link

public static void runLater(java.lang.Runnable runnable) 

運行在JavaFX應用程序線程中指定的Runnable在 未指定的時間未來。這種方法,可以從任何線程調用 ,將Runnable發佈到事件隊列,然後立即返回 給調用者。 Runnables以 的順序執行。傳遞給runLater方法的runnable將在執行任何Runnable之前執行 ,然後調用 runLater。

+0

謝謝aksel !!!!!它真的很有幫助! – ashishcloud

+0

很高興聽到。 :) –

相關問題