Reputation: 4314
I am trying to develop a Java code that will move selected data between two JList, As getModel() method will not return the DefaultListModel and explicit casting is also not allowed to List.getModel() method, is it any other possible way to move selected data from one to other JList and vice versa.?
Here is the Exact view of what i am trying...("Add All" and "Remove all" buttons are working well, i am using Arralist for that, but i am finding solution for selected data, includig ordering sequence of adding and removing data)
Code:
For Left JList"
lmLeft = new DefaultListModel();
lstLeft = new javax.swing.JList();
lstLeft.setModel(lmLeft);
For right JList:
lmRight = new DefaultListModel();
lstRight = new javax.swing.JList();
lstRight.setModel(lmRight);
SOLVED : I replaced JList with JTable
Note : We cannot cast DefaultListModel to getList() as it will return AbstractListModel.
Thank you all for your attention.
Upvotes: 1
Views: 6396
Reputation: 774
These are the steps you are looking for:
This is the core method:
/**
* Moves the {@link Entity entities} from one {@link JList} to another.
* Used in the filters context of the app, but made generic and reusable.
*
* @param from {@link JList} to obtain the entities from.
* @param to {@link JList} to add the obtained entities.
* @param <E> Class of the elements loaded in the {@link JList}.
* @throws IllegalArgumentException - if any the provided parameters are null.
*/
public <E> void moveSelectedTo(JList from, JList to) {
String logID="::moveSelectedTo(from,to): ";
//log.trace("{}Start", logID);
//checkNotNull(from,to);
try{
List<E> entitiesToTransition=from.getSelectedValuesList();
if(entitiesToTransition==null || entitiesToTransition.isEmpty()) return;
DefaultListModel fromModel = (DefaultListModel) from.getModel();
for (E entity: entitiesToTransition) fromModel.removeElement(entity);
List<E> previouslyLoadedEntities=new ArrayList<>();
ListModel<E> model=(to.getModel());
for (int i = 0; i < model.getSize(); i++) previouslyLoadedEntities.add(model.getElementAt(i));
DefaultListModel<E> dlm=new DefaultListModel<>(){{
addAll(previouslyLoadedEntities);
addAll(entitiesToTransition);
}};
to.setModel(dlm);
log.trace("{}Finish", logID);
} catch (Exception e) {
throw new RuntimeException("Impossible to transition the entities from one JList to another ", e);
}
}
Upvotes: 0
Reputation: 1592
Your solution should be very straight forward like:
And for doing Add All and Remove All those should be without question very simple. JList class gives you methods to perform all of the above.
JList.getSelectedIndices() will return an array of int (int[]) of all indexes that have been selected. So you would go through the list and take the items at those indexes and add them to your Right List.
List<Object> myItemsForRightList = new ArrayList<Object>();
int[] selectedIndexes = jListLeft.getSelectedIndicies();
for(int i=0; i < selectedIndexes.length; i++) {
Object whatever = jListLeft.getElementAt(selectedIndexes[i]);
((DefaultListModel)jListRight.getModel()).addElement(whatever);
}
I suggest you also look at what the API has to offer @ http://docs.oracle.com/javase/6/docs/api/javax/swing/JList.html
--- EDIT ---
Just because no complete source code was provided, I am re-posting the code above with JDK 7 in mind
public static void main(String ... args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultListModel<String> defaultListModel = new DefaultListModel<String>();
defaultListModel.addElement("Bla bla bla");
defaultListModel.addElement("Ble ble ble");
defaultListModel.addElement("Blo blo blo");
final JList<String> list = new JList<String>();
list.setPreferredSize(new Dimension(400, 200));
list.setModel(defaultListModel);
JButton button = new JButton("Add");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
((DefaultListModel<String>)list.getModel()).addElement("New one added");
}
});
frame.setLayout(new BorderLayout(5, 5));
frame.add(new JScrollPane(list), BorderLayout.NORTH);
frame.add(button, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
Upvotes: 1