javaFX中的observablelist更新问题

时间:2012-12-12 06:21:41

标签: java listview javafx

我正在使用JavaFX创建一个通用聊天应用程序。我使用SceneBuilder创建了一个listView,并使用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()方法正常工作。为什么呢?

1 个答案:

答案 0 :(得分:3)

UI只能由gui-thread更新。

使用:

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

为什么initialize()方法有效可能是因为它已经从FX应用程序线程调用

来自javadoc link

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

在JavaFX Application Thread上运行指定的Runnable   未来的未指定时间。这种方法可以从中调用   任何线程,都会将Runnable发布到事件队列然后返回   立即给来电者。 Runnables按顺序执行   他们被张贴了。传递给runLater方法的runnable将是   在任何Runnable传入后续调用之前执行   runLater。