JaX
JaX

Reputation: 187

compilation error in adding an element from a jlist

I'm trying to add element from a Jlist to another , append if this the correct term while searching i found this code and tried it but it doesn't work

ListModel made_model = made_list.getModel(); // 1

Object[] orig_sel = orig_list.getSelectedItems(); // 2

Object[] new_made_model = new Object[made_model.size() + orig_sel.length]; // 3

// this block is 4
int i = 0;
for(;i < made_model.size(); i++) 
    new_made_model[i] = made_model.getElementAt(i);
for(; i < new_made_model.length; i++) 
    new_made_model[i] = orig_sel[i - made_model.size());

made_model.setListData(new_made_model); // 5

the error is in this line

Upvotes: 1

Views: 622

Answers (1)

camickr
camickr

Reputation: 324108

setListData() is a method of JList, not of ListModel. You can't cast a ListModel to a JList.

Your code should be:

madeList.setListData( newMadeModel );

Edit:

Instead of playing with Arrays to create a new model just use a DefaultListModel:

DefaultListModel model = new DefaultListModel();

Then you can add Objects directly to the model without using indexes:

model.addElement(...);

When you are finished you add the model to the list:

list.setModel( model );

This way you are less likely to make a mistake when playing with the indexes of the 3 Arrays.

If you need more help then accept this answer (since it was about a compile error) and post a new question with a proper SSCCE that demontrates the problem.

Upvotes: 4

Related Questions