Using ListProperty in JavaFX
Asked Answered
W

1

10

I define a class with the list in it. I am trying to initialize the list in constructor with transmitted list:

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; }
}

The class Priority consists in two fields and their getters :

public IntegerProperty rate;  
public StringProperty speciality;

Could it be that I am not using ListProperty properly?

When I try to create object :

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
Windsucking answered 17/3, 2013 at 5:24 Comment(4)
What is your problem ?Housewifely
When I try to create new object I achieve mistake : "Caused by: java.lang.UnsupportedOperationException"Windsucking
Please complete your question with the full stacktrace.Housewifely
have the same problem with add method, can someone explain why its not working?Meridional
H
25

Try to initialize the SimpleListProperty in this way :

public Person(int id, List<Priority> list) {
    ...
    ObservableList<Priority> observableList = FXCollections.observableArrayList(list)
    this.choice = new SimpleListProperty<Priority>(observableList);
}
Housewifely answered 17/3, 2013 at 17:43 Comment(2)
Thanks, it works! May I ask one more question - how do I suppose to retrieve member of this list? For example to retrieve id I use: public IntegerProperty idProperty() { return id; }Windsucking
A SimpleListProperty is a java.util.List implementation, so you can retreive the member as usual.Housewifely

© 2022 - 2024 — McMap. All rights reserved.