Moving selected data between two JList

巧了我就是萌 提交于 2019-12-12 05:30:12

问题


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.


回答1:


Your solution should be very straight forward like:

  1. Get all selected items from Left List
  2. Add selected items from Left List to Right List

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);
   }


来源:https://stackoverflow.com/questions/19223366/moving-selected-data-between-two-jlist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!