• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add an ArrayList to JList (java 1.7);  RSS feed

 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I'm having trouble adding an ArrayList to JList In java 1.7
I know I have to add a generic type parameter to JList but I can't find the correct syntax.

all I can get to work (with a warning) Is JList list = new Jlist(myArrayList.toArray());

I've tried JList<String> list = new JList<String>() but I can't figure out how to add the ArrayList to that.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your ArrayList look like? Is it an ArrayList<String>?

The problem with myArrayList.toArray(); is that it returns an Object[] and not a String[], which is what the JList<String> constructor expects.

But ArrayList has another toArray method, one which you have to pass an array of the right type. So you can do this:
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for replying.Makes sense now.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!