8

I'm trying to get all the items in a table view and place them in an array list for further processing. This is what I'm trying to achieve but obviously it won't work.

ArrayList<Consultation> showing = consultationTable.getItems();
8
  • 4
    If you must have an ArrayList, then ArrayList<Consultation> showing = new ArrayList<Consultation>(consultationTable.getItems())
    – khelwood
    Oct 5, 2016 at 11:28
  • what about ArrayList<Consultation> showing = new ArrayList<>(consultationTable.getItems()); ?
    – f1sh
    Oct 5, 2016 at 11:28
  • 3
    Is there a reason why specifically need an ArrayList? Why not List<Consultation> showing = ...;? Program to an interface, not an implementation.
    – Jesper
    Oct 5, 2016 at 11:30
  • 1
    Why on earth do you need that? Unless you went out of your way to do otherwise, the ObservableList is backed by an ArrayList anyway (the default way to create one is ObservableList<Consultation> obsList = FXCollections.observableArrayList() or similar.). So what is wrong with List<Consultation> showing = consultationTable.getItems();. Is there really some ArrayList-specific API you need?
    – James_D
    Oct 5, 2016 at 11:48
  • 1
    I wish that was the case, I didn't own the other class another developer created it and was using it for a database method. Too much would need to be changed on his end
    – Philayyy
    Oct 8, 2016 at 3:55

4 Answers 4

9

Nice and recommended solution:

List<Consultation> showing = provider.getItems();

Solution to use only if necessary:

    List<Consultation> consultations = provider.getItems();
    ArrayList<Consultation> showing;
    if (consultations instanceof ArrayList<?>) {
        showing = (ArrayList<Consultation>) consultations;
    } else {
        showing = new ArrayList<>(consultations);
    }

If for some reason you need to use an ArrayList method that is not in the List or ObservableList interface (I cannot readily think of why), you may use the latter.

6

A simple method using Stream class

List<T> list = ObservableList<T>.stream().collect(Collectors.toList());
1

Try this

new ArrayList<>(consultationTable.getItems())
0
-2
// To convert an observable list to an array of strings given the following observable list:

// Create these in the class 
ListView<String> myListView = new ListView<>();
ObservableList<String> myList;

// Then define them in the start class myList = FXCollections.observableArrayList(someGivenArray); myListView.setItems(myList);

// Make an array to store list items in the observable list as strings
List<String> myArray = new ArrayList<String>();

// Loop through the observable list and load the string array
    for (int i =0; i<myList.size(); i++){
       myArray.add(myList.get(i));
    }
// Test by printing out to the screen or a text field
    System.out.println(myList.get(0));
    myTextField.setText(myList.get(0));
1
  • 1
    ObservableList is a List. Elements can be added from one list to another at construction time or using the addAll method. Apr 25, 2018 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.