I have to put all elements from a DefaultListModel
(in a listbox) into a List<Object>
. How can I do that in Java?
In Java 1.7, DefaultListModel is genericized. So when you call portalList.getModel (), you're going to get back a DefaultListModel that contains a certain type of object (which is what the <E> is; it's a placeholder for the actual object type.) In the above List<String> example, String is the substitution for E.
List<Object> list = getList (); return (List<Customer>) list; you can always cast any object to any type by up-casting it to Object first. in your case: you must be sure that at runtime the list contains nothing but Customer objects.
you can always cast any object to any type by up-casting it to Object first. in your case: you must be sure that at runtime the list contains nothing but Customer objects. Critics say that such casting indicates something wrong with your code; you should be able to tweak your type declarations to avoid it.
Fields inherited from class javax.swing. AbstractListModel Inserts the specified element at the specified position in this list. Adds the specified component to the end of this list. Returns the current capacity of this list.
Arrays.asList(model.toArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With