微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在JavaFX中使用ListProperty

我定义了一个包含列表的类.我试图用传输列表初始化构造函数中的列表:
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; }
}

类优先级包含两个字段及其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

解决方法

尝试以这种方式初始化SimpleListProperty:
public Person(int id,List<Priority> list) {
    ...
    ObservableList<Priority> observableList = FXCollections.observableArrayList(list)
    this.choice = new SimpleListProperty<Priority>(observableList);
}

原文地址:https://www.jb51.cc/java/128095.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐