Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
65 views
in Technique[技术] by (71.8m points)

Convert DefaultListModel to List in Java

I'm making a Swing application that includes a JList and all the objects in the list are stored in a DefaultListModel. At the same time I need to pass the list to a method that receives as input a List. How can i convert a DefaultListModel to a List ?

This is the error i got:

The method computeSpending(List<Subscription>) in the type SubscriptionSpending is not applicable for the arguments (DefaultListModel<Subscription>)
question from:https://stackoverflow.com/questions/65869697/convert-defaultlistmodel-to-list-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How can i convert a DefaultListModel to a List ?

Arrays.asList(defaultListModel.toArray());


Description:

DefaultListModel is not a subclass of List. So it is not directly possible to pass that as a List argument. Instead you can change the parameter type to ListModel<E>. Such as:

computeSpending(ListModel<Subscription>)

Also, If you must need to pass the DefaultListModel<Subscription> into a List, you can use:

List<Subscription> asList = Arrays.asList(defaultListModel.toArray());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...