在JavaFX中使用ListProperty
我定义了一个包含列表的类。我正在尝试使用传输的列表初始化构造函数中的列表:在JavaFX中使用ListProperty,list,javafx-2,listproperty,List,Javafx 2,Listproperty,我定义了一个包含列表的类。我正在尝试使用传输的列表初始化构造函数中的列表: public class Person { public IntegerProperty id; public ListProperty<Priority> choice; public Person(int id, List<Priority> list) { this.id = new SimpleIntegerProperty(id);
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; }
}
可能是我没有正确使用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
应用程序启动方法中出现异常
线程“main”java.lang.RuntimeException中的异常:应用程序启动方法中的异常
位于com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:403)
访问com.sun.javafx.application.launchempl.access$000(launchempl.java:47)
位于com.sun.javafx.application.launchempl$1.run(launchempl.java:115)
运行(Thread.java:722)
原因:java.lang.UnsupportedOperationException
在java.util.AbstractList.add处(AbstractList.java:148)
添加(AbstractList.java:108)
位于java.util.AbstractCollection.addAll(AbstractCollection.java:334)
位于javafx.beans.binding.ListExpression.addAll(ListExpression.java:280)
at Person.setAllchoice(Person.java:24)
at Person.(Person.java:17)
位于Distribution.ReadDataFromFile(Distribution.java:85)
在Distribution.start(Distribution.java:28)
位于com.sun.javafx.application.launchempl$5.run(launchempl.java:319)
位于com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
位于com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
位于com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
位于java.security.AccessController.doPrivileged(本机方法)
位于com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
位于com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:76)
在com.sun.glass.ui.gtk.GtkApplication.\u runLoop(本机方法)
位于com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
... 还有一个
尝试以下方式初始化
SimpleListProperty
:
public Person(int id, List<Priority> list) {
...
ObservableList<Priority> observableList = FXCollections.observableArrayList(list)
this.choice = new SimpleListProperty<Priority>(observableList);
}
公众人物(内部id,列表){
...
ObservableList ObservableList=FXCollections.observableArrayList(列表)
this.choice=新的SimpleListProperty(observableList);
}
你的问题是什么?当我尝试创建新对象时,我犯了一个错误:“起因:java.lang.UnsupportedOperationException”请用完整的stacktrace完成你的问题。如果add方法也有同样的问题,有人能解释为什么它不工作吗?谢谢,它工作了!我可以再问一个问题吗?我该如何检索此列表中的成员?例如,要检索我使用的id:public IntegerProperty idProperty(){return id;}ASimpleListProperty
是一个java.util.List
实现,因此可以像往常一样检索成员。
public Person(int id, List<Priority> list) {
...
ObservableList<Priority> observableList = FXCollections.observableArrayList(list)
this.choice = new SimpleListProperty<Priority>(observableList);
}