JList自定义选择行为 - 包括从单击项目到列表末尾的所有内容

时间:2015-07-02 15:00:32

标签: java swing jlist listselectionlistener

我使用 JList 来记录&显示我的数据的更改。我对能够"撤消"感兴趣那些变化。出于这个原因,我想确保无论用户点击列表,选择包括从该项目到列表末尾的所有内容。我原以为

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

会这样做,但它实际上被忽略了。从OracleJava2sRiga U编写 ListSelectionListeners 的教程都使用完全相同(无用的)示例。有没有人有更好的方法?提前谢谢!

1 个答案:

答案 0 :(得分:1)

这非常出色!谢谢,@ 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);
    }

}