How to distinguish DefaultListModel in List <Object>?

I need to put all the items from DefaultListModel(in the list) into List<Object>. How can I do this in Java?

+5
source share
3 answers

If you want to make a copy of the content, you can use DefaultListModel.toArrayto get the data and create your favorite implementation Listwith it. Alternatively, you can run the loop ListModel.getElementAt ListModel.getSizeonce.

If you need a live connection between collections, not a copy, use AbstractList:

 public static List<Object> asList(final DefaultListModel model) {
     return new AbstractList<Object>() {
          @Override public Object get(int index) {
              return        model.getElementAt(index);
          }
          ...
     };
 }

You might want to put it Class.castthere, but there is a problem with the fact that Swing types are not common.

+7
source
Arrays.asList(model.toArray());
+11
source

API, elements() , . DefaultListModel , Collections.

OR use what jarnbjo suggested!

+1
source

All Articles