【问题标题】:Using ListProperty in JavaFX在 JavaFX 中使用 ListProperty
【发布时间】:2013-03-17 05:24:00
【问题描述】:

我定义了一个包含列表的类。我正在尝试使用传输列表初始化构造函数中的列表:

public class Person {
    public IntegerProperty id;
    public ListProperty<Priority> choice;

    public Person(int id, List<Priority> list) {
        this.id = new SimpleIntegerProperty(id);
        this.choice = new SimpleListProperty<Priority>();
        for (int i = 0; i < 5; i++) {
            this.choice.add(list.get(i));
        }
    }

    public IntegerProperty idProperty() { return id; }
    public ListProperty<Priority> choiceProperty() { return choice; }
}

Priority 类包含两个字段及其 getter:

public IntegerProperty rate;  
public StringProperty speciality;

会不会是我没有正确使用ListProperty

当我尝试创建对象时:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start     method
  at  com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
  at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
  at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
  at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.UnsupportedOperationException
  at java.util.AbstractList.add(AbstractList.java:148)
  at java.util.AbstractList.add(AbstractList.java:108)
  at java.util.AbstractCollection.addAll(AbstractCollection.java:334)
  at javafx.beans.binding.ListExpression.addAll(ListExpression.java:280)
  at Person.setAllchoice(Person.java:24)
  at Person.<init>(Person.java:17)
  at Distribution.ReadDataFromFile(Distribution.java:85)
  at Distribution.start(Distribution.java:28)
  at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
  at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
  at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
  at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
  at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
  at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
  at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
  ... 1 more

【问题讨论】:

  • 你的问题是什么?
  • 当我尝试创建新对象时出现错误:“Caused by: java.lang.UnsupportedOperationException”
  • 请使用完整的堆栈跟踪完成您的问题。
  • add 方法有同样的问题,谁能解释为什么它不起作用?

标签: list javafx-2 listproperty


【解决方案1】:

尝试以这种方式初始化SimpleListProperty

public Person(int id, List<Priority> list) {
    ...
    ObservableList<Priority> observableList = FXCollections.observableArrayList(list)
    this.choice = new SimpleListProperty<Priority>(observableList);
}

【讨论】:

  • 谢谢,它有效!我可以再问一个问题 - 我应该如何检索此列表的成员?例如,要检索我使用的 id: public IntegerProperty idProperty() { return id; }
  • SimpleListPropertyjava.util.List 实现,因此您可以照常检索成员。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多