Reputation: 187
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
made_model.setListData(new_made_model); // 5 it tells me to cast made_model to Jlist , which i did but then while running the class , i get this error
javax.swing.JList$1 cannot be cast to javax.swing.JList
Upvotes: 1
Views: 622
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