Results 1 to 2 of 2
  1. #1
    sjs
    sjs is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default JList.getSelectedValues()

    JList.getSelectedValues() returns values sorted by increasing index order. Is there a way to return the values in the order they were selected? Or do I have to write my own listener and build the array? Seemed like a JTree returns multiple selections in the order they were selected, is that true?

    For example if list contains:
    1
    2
    3
    4
    5

    And 5 is selected first, then 3, and then 1. I want the array to be [5,3,1], and not [1,3,5].

    Scott

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Mhm, I have never been done before, but my workaround is:

    - override the getSelectedValues() method in JList
    - write your own ListSelectionModel (perhaps extend DefaultListSelectionModel)

    Java Code:
    class SelectModel extends DefaultListSelectionModel {
    
    	private List<Integer> indices = new ArrayList<Integer>();
    
    	/**
    	 * @return the indices
    	 */
    	public List<Integer> getIndices() {
    		return indices;
    	}
    
    	@Override
    	public void addSelectionInterval(int index0, int index1) {
    		addIndices(index0, index1);
    		super.addSelectionInterval(index0, index1);
    	}
    
    	@Override
    	public void setSelectionInterval(int index0, int index1) {
    		indices.clear();
    		addIndices(index0, index1);
    		super.setSelectionInterval(index0, index1);
    	}
    
    	private void addIndices(int index0, int index1) {
    		if (index0 < index1) {
    			for (int i = index0; i <= index1; i++) {
    				indices.add(i);
    			}
    		} else {
    			for (int i = index0; i >= index1; i--) {
    				indices.add(i);
    			}
    		}
    	}
    }
    and
    Java Code:
    			@Override
    			public Object[] getSelectedValues() {
    				SelectModel selectmodel = (SelectModel) getSelectionModel();
    				List<Integer> indices = selectmodel.getIndices();
    				ListModel model = getModel();
    				Object[] obj = new Object[indices.size()];
    				int i = 0;
    				for (Integer index : indices) {
    					obj[i++] = model.getElementAt(index);
    				}
    				return obj;
    			}

    simple demo:
    Java Code:
    	public static void main(String[] args) throws Exception {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				JFrame frame = new JFrame();
    				JPanel panel = new JPanel();
    				final JList list = new JList() {
    					{
    						DefaultListModel model = new DefaultListModel();
    						model.addElement(1);
    						model.addElement(2);
    						model.addElement(3);
    						model.addElement(4);
    						setModel(model);
    					}
    
    					@Override
    					public Object[] getSelectedValues() {
    						SelectModel selectmodel = (SelectModel) getSelectionModel();
    						List<Integer> indices = selectmodel.getIndices();
    						ListModel model = getModel();
    						Object[] obj = new Object[indices.size()];
    						int i = 0;
    						for (Integer index : indices) {
    							obj[i++] = model.getElementAt(index);
    						}
    						return obj;
    					}
    
    				};
    				panel.add(list);
    				JButton btn = new JButton("get selected values");
    				btn.addActionListener(new ActionListener() {
    					@Override
    					public void actionPerformed(ActionEvent e) {
    						System.out.println(Arrays.toString(list.getSelectedValues()));
    					}
    				});
    				panel.add(btn);
    				list.setSelectionModel(new SelectModel());
    				frame.add(panel);
    				frame.pack();
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.setLocationRelativeTo(null);
    				frame.setVisible(true);
    			}
    		});
    	}
    EDIT: deselect/removeIndexInterval : its your part ^^
    Last edited by eRaaaa; 10-13-2010 at 01:08 AM.

Similar Threads

  1. JList
    By nawl in forum New To Java
    Replies: 2
    Last Post: 05-21-2010, 07:37 AM
  2. How to use a JList?
    By glhansen in forum New To Java
    Replies: 3
    Last Post: 03-24-2009, 11:27 AM
  3. JList
    By pinks_70986 in forum New To Java
    Replies: 1
    Last Post: 02-12-2009, 09:36 AM
  4. About JList
    By hungleon88 in forum Advanced Java
    Replies: 5
    Last Post: 08-30-2008, 10:24 PM
  5. Help with JList
    By Albert in forum NetBeans
    Replies: 1
    Last Post: 07-13-2007, 04:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •