javaswingjlistlistselectionlistener

JList custom selection behaviour - to include everything from the item clicked to the end of the list


I'm using a JList to record & display changes to my data. I'm interested in being able to "undo" those changes. For that reason I would like to ensure that, whatever the user clicks on the list, the selection includes everything from that item to the end of the list. I had thought that

    lstLog.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent lse) {
            if(!lse.getValueIsAdjusting())
                if((lse.getFirstIndex() == (lstLog.getModel().getSize() - 1)) || (lse.getLastIndex() == (lstLog.getModel().getSize() - 1))) {
//                  this.valueChanged(lse);
                }
                else {
                    int lowestSelection; 
                    if(lse.getFirstIndex() < 0) lowestSelection = lse.getLastIndex();
                    else if (lse.getLastIndex() < 0) lowestSelection = lse.getFirstIndex();
                    else lowestSelection = ((lse.getFirstIndex() < lse.getLastIndex()) ? lse.getLastIndex() : lse.getFirstIndex());
                    lstLog.setSelectionInterval(lowestSelection, lstLog.getModel().getSize() - 1);
                }
        }
    });

would do it, but it's practically ignored. Tutorials on writing ListSelectionListeners from Oracle, Java2s and Riga U all use exactly the same (unhelpful) example. Does anyone have a better way? Thanks in advance!


Solution

  • This works brilliantly! Thanks, @mKorbel !

    import javax.swing.ListSelectionModel;
    import javax.swing.DefaultListSelectionModel;
    import javax.swing.JList;
    import javax.swing.ListModel;
    import javax.swing.event.ListSelectionListener;
    
    /**
    *
    * @author MaskedCoder
    */
    public class ActionListSelectionModel extends DefaultListSelectionModel implements ListSelectionModel {
        ListModel thisModel;
    
        public ActionListSelectionModel(JList iniList) {
            super();
            thisModel = iniList.getModel();
            super.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            super.setAnchorSelectionIndex(thisModel.getSize() - 1);
            // maybe add datalistener to update AnchorSelection when items added/removed
        }
    
        private int betterOf(int index0, int index1) {
            if(index0 < 0) return index1;
            else if (index1 < 0) return index0;
            else return ((index0 < index1) ? index1 : index0);
        }
    
        /*
        * ListSelectionModel implementation
        */
        @Override
        public void setSelectionInterval(int index0, int index1) {
            super.setSelectionInterval(thisModel.getSize() - 1, betterOf(index0, index1));
        }
    
        @Override
        public void addSelectionInterval(int index0, int index1) {
            // don't want to add, so just do same as above
            super.setSelectionInterval(thisModel.getSize() - 1, betterOf(index0, index1));
        }
    
        @Override
        public void setAnchorSelectionIndex(int anchorIndex) {
            super.setAnchorSelectionIndex(thisModel.getSize() - 1);
        }
    
        @Override
        public void insertIndexInterval(int index, int length, boolean before) {
            super.setSelectionInterval(thisModel.getSize() - 1, index);
        }
    
        @Override
        public void removeIndexInterval(int index0, int index1) {
            // do nothing.  Only (1) interval and it will never be removed.
        }
    
        @Override
        public void setSelectionMode(int selectionMode) {
            super.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        }
    
    }