mike b
mike b

Reputation: 57

how do i move selected elements from one Jlist to another Jlist in a different Class

i have created a menu where i got 4 classes. i created a Jlist for each course meal which contains different elements. How do i know get these selected items to display in another Jlist in a different Class which also contains a Jlist.

Upvotes: 1

Views: 903

Answers (1)

Jonah
Jonah

Reputation: 1003

You should take a look at Default List Models

You can create a DLM and share it's contents between other DLM's. You could start to go about doing it by adding something like this in your first class

DefaultListModel dlm = new DefaultListModel();
JList list1 = new JList(dlm);

You could then add the default elements to the DLM and create a function in your other class that assigns that DLM to your other JList

public static void setDLM(DefaultListModel dlm)
{
    list2.setModel(dlm);
}

You should then static import the setDLM() method, and execute the following in your buttons ActionListener

DefaultListModel<String> dlm2 = new DefaultListModel<>();

for(String item : list1.getSelectedValuesList())
{
   dlm2.addElement(item);
}

setDLM(dlm2);

Upvotes: 1

Related Questions