Reputation: 11
I want to transfer the selected objects from one JList to another JList, say List1 and List2.
Upvotes: 0
Views: 5516
Reputation: 321
Here I am showing you example of transferring name of cities from list
to list_1
. You will receive output something like this:
Code for this is :
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.DefaultListModel;
import java.awt.Font;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ListTransfer {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ListTransfer window = new ListTransfer();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ListTransfer() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
String[] values = new String[] {"Surat", "Ahmedabad", "Vadodra", "Anand", "Bharuch", "Maninagar", "Mumbai", "Pune", "Hydrabad", "Banglore"};
DefaultListModel<Object> model_list = new DefaultListModel<Object>();
DefaultListModel<Object> model_list_1 = new DefaultListModel<Object>();
for (String value : values) {
model_list.addElement(value);
}
JList<Object> list = new JList<Object>();
list.setFont(new Font("Lucida Grande", Font.PLAIN, 21));
list.setBounds(17, 41, 174, 194);
list.setModel(model_list);
JScrollPane listScroller = new JScrollPane();
listScroller.setLocation(15, 43);
listScroller.setSize(174, 194);
listScroller.setViewportView(list);
list.setLayoutOrientation(JList.VERTICAL);
frame.getContentPane().add(listScroller);
JList<Object> list_1 = new JList<Object>();
list_1.setFont(new Font("Lucida Grande", Font.PLAIN, 21));
list_1.setBounds(262, 41, 174, 194);
list_1.setModel(model_list_1);
JScrollPane listScroller1 = new JScrollPane();
listScroller1.setLocation(262, 41);
listScroller1.setSize(174, 194);
listScroller1.setViewportView(list_1);
list_1.setLayoutOrientation(JList.VERTICAL);
frame.getContentPane().add(listScroller1);
JButton shiftbutton = new JButton(">>>");
shiftbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
list.getSelectedValuesList().stream().forEach((data) -> {
model_list_1.addElement(data);
model_list.removeElement(data);
});
}
});
shiftbutton.setBounds(192, 116, 66, 52);
frame.getContentPane().add(shiftbutton);
}
}
Upvotes: 4
Reputation: 31
try something like this. it will work fine
DefaultListModel dlm = new DefaultListModel();
jButtonActionPerformed {
jList2.setModel(dlm);
jList1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
for (Iterator it = jList1.getSelectedValuesList().iterator(); it.hasNext();) {
String sel = (String) it.next();
if (dlm.contains(sel)) {
} else {
dlm.addElement(sel);
}
}
}
Upvotes: 0
Reputation: 87
as you don't show any code, all I can say is this:
assuming you are using, for example, an arrayList of custom objects as a source of data for each list, you could have a button that when pressed, gets the selected item index, grabs that object and then you add it to the other list.
method for the pressed button event (){
arrayList2.add(arrayList1.get(jList1.getSelectedIndex());
}
after this you can just reload the model on jlist2 so it shows the new data.
hope it helps
Upvotes: 0
Reputation: 347194
Start by taking a look at JList#getSelectedValuesList
(or JList#getSelectedValues
if you're using Java 6 or earlier)
You will then need to apply these values to the second JList
's model. How you do this will depend on the model that you are already using...
Upvotes: 1