e779. 获得JList中的已选项

Posted borter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了e779. 获得JList中的已选项相关的知识,希望对你有一定的参考价值。

The following methods return the indices of the selected items:

    // To create a list, see e774 创建JList组件
    
    // Get the index of all the selected items
    int[] selectedIx = list.getSelectedIndices();
    
    // Get all the selected items using the indices
    for (int i=0; i<selectedIx.length; i++) {
        Object sel = list.getModel().getElementAt(selectedIx[i]);
    }
    
    // Get the index of the first selected item
    int firstSelIx = list.getSelectedIndex();
    
    // Get the index of the last selected item
    int lastSelIx = list.getMaxSelectionIndex();
    
    // Determine if the third item is selected
    int index = 2;
    boolean isSel = list.isSelectedIndex(index);
    
    // Determine if there are any selected items
    boolean anySelected = !list.isSelectionEmpty();

The following methods return the selected item objects:

    // Get the first selected item
    Object firstSel = list.getSelectedValue();
    
    // Get all selected items without using indices
    Object[] selected = list.getSelectedValues();

 

Related Examples

以上是关于e779. 获得JList中的已选项的主要内容,如果未能解决你的问题,请参考以下文章

e774. 创建JList组件

java和sql到JList

检索 JList 项的数组数据

滑块菜单片段中的可交换选项卡

e785. 监听JList中项的变动

如何生成具有交替颜色的 Jlist