public void select(Object item) { IndividualListSelectionModel targetModel = (IndividualListSelectionModel) target.getModel(); IndividualListSelectionModel sourceModel = (IndividualListSelectionModel) source.getModel(); if (!sourceModel.contains(item)) return; sourceModel.remove(item); targetModel.add(item); }
private void moveOne( IndividualListSelectionModel sourceModel, IndividualListSelectionModel targetModel, JList source) { int[] indices = source.getSelectedIndices(); if (indices.length == 0) return; for (int i = indices.length - 1; i >= 0; i--) { Object object = sourceModel.getObjectAt(indices[i]); sourceModel.remove(indices[i]); targetModel.add(object, targetModel.getSize()); } }
private void moveAll( IndividualListSelectionModel sourceModel, IndividualListSelectionModel targetModel) { if (sourceModel.getSize() == 0) return; int size = sourceModel.getSize(); for (int i = size - 1; i >= 0; i--) { Object object = sourceModel.getObjectAt(i); sourceModel.remove(i); targetModel.add(object, targetModel.getSize()); } }
protected void init() { source = new JList(); source.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); target = new JList(); target.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); source.setModel(new IndividualListSelectionModel(null, attribute, sorted)); target.setModel(new IndividualListSelectionModel(null, attribute, sorted)); final IndividualListSelectionModel sourceModel = (IndividualListSelectionModel) source.getModel(); final IndividualListSelectionModel targetModel = (IndividualListSelectionModel) target.getModel(); sourceModel.addListDataListener( new ListDataListener() { public void contentsChanged(ListDataEvent arg0) {} public void intervalAdded(ListDataEvent e) { List<Object> sourceObjects = sourceModel.getAll(); List<Object> targetObjects = targetModel.getAll(); for (Object sobj : sourceObjects) if (targetObjects.contains(sobj)) targetModel.remove(sobj); } public void intervalRemoved(ListDataEvent arg0) {} }); targetModel.addListDataListener( new ListDataListener() { public void contentsChanged(ListDataEvent arg0) {} public void intervalAdded(ListDataEvent e) { List<Object> sourceObjects = sourceModel.getAll(); List<Object> targetObjects = targetModel.getAll(); for (Object tobj : targetObjects) if (sourceObjects.contains(tobj)) sourceModel.remove(tobj); } public void intervalRemoved(ListDataEvent arg0) {} }); oneToSource = new JButton( new ImageIcon( this.getClass().getClassLoader().getResource(GoitacaResourceBox.getPrevIcon()))); oneToSource.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { oneToSource(); } }); allToSource = new JButton( new ImageIcon( this.getClass().getClassLoader().getResource(GoitacaResourceBox.getFirstIcon()))); allToSource.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { allToSource(); } }); oneToTarget = new JButton( new ImageIcon( this.getClass().getClassLoader().getResource(GoitacaResourceBox.getNextIcon()))); oneToTarget.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { oneToTarget(); } }); allToTarget = new JButton( new ImageIcon( this.getClass().getClassLoader().getResource(GoitacaResourceBox.getLastIcon()))); allToTarget.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { allToTarget(); } }); }