Frkn
Frkn

Reputation: 157

java swing jlist data transferring

jList2.setListData(jList1.getSelectedValues());
I used this, to transfer data from a jlist1 to jlist2. But I want to remove the transferred data from jlist1. How it can be.

Upvotes: 1

Views: 428

Answers (2)

mKorbel
mKorbel

Reputation: 109813

there are two ways

  • to look at Drag an Drop tutorial on Oracle pages

  • on mouseDragged to remove selectedItem from one JList and to add to the seconds, notice this idea required better knowledge about Java Swing

Upvotes: 1

James
James

Reputation: 2542

First: getSelectedValues() is deprecated, use getSelectedValuesList() to get them as a List using generics.

Second, create a DefaultListModel before creating your JList so you can add/remove to/from it, e.g.

DefaultListModel<String> model = new DefaultListModel<String>();
DefaultListModel<String> model2 = new DefaultListModel<String>();
JList<String> list1 = new JList<>(model);
JList<String> list2 = new JList<>(model2);

...

for(String s : list1.getSelectedValuesList()){
    model2.addElement(s);
    model.removeElement(s);
}

Remember that changes to the list and models must be performed on the event dispatch thread

Upvotes: 1

Related Questions