Creating an Observable List/Collection

前端 未结 1 1105
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 11:11

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

相关标签:
1条回答
  • 2021-01-30 11:35

    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());
    
    0 讨论(0)
提交回复
热议问题