KyleTheSnake
KyleTheSnake

Reputation: 3

How to copy data from one JList to Another JList

So, I have a two tabs, both of which have JList's and they both have an arraylist in which their data is stored, now I need to find a way that will enable be to copy data from one JList in one tab to another JList in another tab.

Thansk

Upvotes: 0

Views: 4880

Answers (2)

mike
mike

Reputation: 1

Well, first you have to set the Models for each jList

DefaultListModel listModel = new DefaultListModel();
DefaultListModel listModel2 = new DefaultListModel();

I think that you have stored the data into the first jList, so you just pass it into the second jList as follows:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)     {                                         
   int index=LV.getSelectedIndex();
   listModel2.addElement(LV.getSelectedValue());
   LV2.setModel(listModel2);
   listModel.remove(index);
} 

Upvotes: 0

Mattias Isegran Bergander
Mattias Isegran Bergander

Reputation: 11909

Depends on what you have and intend exactly, this works, but then they would share the ListModel, not copy:

list2.setModel(list1.getModel());

Else loop through the elements from one model and add into to the other.

ListModel model = list1.getModel();
DefaultListModel list2Model = new DefaultListModel();
for (int i=0; i<model.getSize(); i++) {
  list2Model.addElement(model.elementAt(i);
}

list2.setModel(list2Model);

Upvotes: 2

Related Questions