Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Observable List/Collection

I'm trying to create a ChoiceBox in JavaFX 8, which requires a Collection. I can't figure out how to create a Collection though... If I try:

 ObservableList<String> list = new ObservableList<String>();

I get an error saying I can't instantiate ObservableList because it's abstract. Understandable. If I look at the doc for ObservableList I can see that SortedList implements ObservableList, but I can't do:

 ObservableList<String> list = new SortedList<String>();

Because there's no applicable constructor. Apparently I need to have an ObservableList to pass to the SortedList, which is odd because I can't create an ObservableList.

constructor SortedList.SortedList(ObservableList<? extends String>,Comparator<? super String>) is not applicable
  (actual and formal argument lists differ in length)
constructor SortedList.SortedList(ObservableList<? extends String>) is not applicable
  (actual and formal argument lists differ in length)

I'm not sure how to decipher that. If I try

 ObservableList<String> list = new SortedList<SortedList<String>>();
 //or
 ObservableList<String> list = new SortedList<ObservableList<String>>();

out of desperation, I get an even more convoluted error.

    SortedList<String> list = new SortedList<String>();

doesn't work either. Somehow this works (but apparently uses an unsafe operation):

ChoiceBox box = new ChoiceBox(FXCollections.observableArrayList("Asparagus", "Beans", "Broccoli", "Cabbage" , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom" , "Pepper", "Radish", "Shallot", "Spinach", "Swede" , "Turnip"));

So I tried:

 ObservableList<string> list = new FXCollections.observableArrayList("Asparagus", "Beans", "Broccoli", "Cabbage" , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom" , "Pepper", "Radish", "Shallot", "Spinach", "Swede" , "Turnip");

But no luck there either. I'm super confused, doing the same thigns over and over in an endless loop of trying to understand this. The documentation I've found shows examples that don't help, or no examples. The official documentation is useless too:

Suppose, for example, that you have a Collection c, which may be a List, a Set, or another kind of Collection. This idiom creates a new ArrayList (an implementation of the List interface), initially containing all the elements in c.

 List<String> list = new ArrayList<String>(c);

So to create ArrayList, an implementation of List, I need to have a List. the reason I went to the documentation in the first place was to learn how to make what they're assuming I have. I'm lost. Help?

like image 521
LilSweden Avatar asked Oct 04 '14 17:10

LilSweden


1 Answers

Use the factory methods in FXCollections:

ObservableList<String> list = FXCollections.observableArrayList();

The unsafe operation in your choice box constructor is because you haven't specified the type for the choice box:

ChoiceBox<String> box = new ChoiceBox<>(FXCollections.observableArrayList("Asparagus", "Beans", "Broccoli", "Cabbage" , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom" , "Pepper", "Radish", "Shallot", "Spinach", "Swede" , "Turnip"));

and the error from SortedList is because there is no constructor taking no arguments. (Again, refer to the javadocs.) There are two constructors: the simplest one takes a reference to an ObservableList (the list for which the sorted list will provide a sorted view). So you would need something like

SortedList<String> sortedList = new SortedList<>(list);

or

SortedList<String> sortedList = new SortedList<>(FXCollections.observableArrayList());
like image 175
James_D Avatar answered Oct 20 '22 17:10

James_D