0

How to move an item from one list/arraylist to another list/arraylist?

Here's what I am required to do

  • Add songs from a text field to a music library ArrayList.
  • Select songs in the List Box and add them to the Playlist.
  • Select songs in the Playlist List Box and remove them from the Playlist.
  • Sort the songs in the Playlist.

I finally figured out how to add items to an arraylist and display them in a listbox. Now I need to know how to select them and move them to another list/array using a button.

Any ideas?

2
  • Take a look at this which suggest that you need to create a ListModel which uses your List as it's data source. The code your teacher gave is for working with Lists from the collections API, not JList. You may also want to check out How to use lists for more details Commented Jun 21, 2013 at 5:10
  • Thanks, i found out i was supposed to use AWT lists instead of Swing lists. Ive updated my question but i was able to get the first list box to display the array Commented Jun 21, 2013 at 5:47

1 Answer 1

2

It will be easier for you to use ListModel as suggested by MadProgrammer

You can try like this

model = new DefaultListModel<String>();
    for(String str : playlist){
         model.addElement(str);
    }    
    listPlaylist.setModel(model);     
    listPlaylist.setSelectedIndex(0);

You can create two models for library and playlist and perform operations

Sign up to request clarification or add additional context in comments.

3 Comments

Well I dont know hoe to make the arraylist my teacher gave me into an area, heres the code he told me to use for it private ArrayList<String> library = new ArrayList<String>(); And ass for using .setlistdata Its giving me the same error as .add and .setText
I managed to make the arraylist into an array, but now my library.add(txtSong.getText()); isnt working, and highlights .add
check a sample here it will be easier for u....java2s.com/Code/Java/Swing-JFC/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.