【问题标题】:new added element in the top of JList is automatically selected自动选中 JList 顶部新添加的元素
【发布时间】:2020-05-02 11:35:46
【问题描述】:

我正在使用 swing 创建一个 JList,我可以显示和选择多个项目,也可以向其中添加一个新元素。但是,当我选择列表的第一个元素并在顶部添加一个新元素时,我得到了两个选定元素(旧元素和新元素),但是当我将 选择模式更改为单选 em> 它工作正常,是否可以防止这种自动选择,而只是保持旧的选择使用多间隔选择模式? 我使用了这个link,其中包含一个使用 DataEventListner 的示例,但我没有成功找到解决方案。请问有什么帮助吗? 这是我的清单:

 public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H" };
    JFrame frame = new JFrame("Selecting JList");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final DefaultListModel model = new DefaultListModel();
    for (int i = 0, n = labels.length; i < n; i++) {
        model.addElement(labels[i]);
    }
    JList jlist = new JList(model);
    jlist.setSelectionMode(
            ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1, BorderLayout.CENTER);
    JButton jb = new JButton("add F");
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.add(0, "First");
        }
    });
    frame.add(jb,BorderLayout.SOUTH);
    frame.setSize(640, 300);
    frame.setVisible(true);
}

【问题讨论】:

    标签: java swing jlist


    【解决方案1】:

    我看到您基本上复制了您问题中link 中的代码。该示例仅处理单击JButton 时向JList 添加单个元素。它不处理JList 选择[s]。我不认为该示例的作者考虑了当用户选择JList 中的一个或多个元素点击JButton 之前会发生什么。

    我能够重现您问题中描述的行为。这可能是JListListSelectionModel 的实现中的一个错误。我修复它的方法是将代码添加到处理任何现有 JList 选择的方法 actionPerformed()

    这是我修改后的方法actionPerformed()。请注意,所有其余代码均未更改。首先,我保存所有选定行的索引。然后我清除现有的选择。然后我将新元素添加到JList。现在我需要重新选择在添加新元素之前选择的行。但请注意,我需要将每个索引加一,因为索引 0(零)处有一个新元素。

    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int[] indices = jlist.getSelectedIndices();
            jlist.getSelectionModel().clearSelection();
            model.add(0, "First");
            for (int index : indices) {
                jlist.getSelectionModel().addSelectionInterval(index + 1, index + 1);
            }
        }
    });
    

    【讨论】:

    • 应该做这项工作的简单技巧
    【解决方案2】:

    来自JList#setSelectionMode(int)的文档:

    ListSelectionModel.SINGLE_SELECTION - 只能有一个列表索引 一次选择。在这种模式下,setSelectionInterval 和 addSelectionInterval 是等价的,都替换了当前 使用第二个参数表示的索引进行选择( “铅”)。

    试试jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    【讨论】:

    • 我要多选
    • edit您的帖子包含任何基本信息和限制。
    • 我猜你错过了原始问题的这一部分:我可以显示和选择多个项目
    【解决方案3】:

    你可以在你的 jb.addActionListner() 中添加这个:

    int x = jlist.getSelectedIndex();
        jlist.clearSelection();
        jlist.setSelectedIndex(x);
    

    这应该按照你想要的方式工作!

    【讨论】:

    • 我认为这会选择新元素,但我只想保留旧的选择值。
    • 然后就可以得到选中项的索引了:
    • int x = jlist.getSelectedIndex(); jlist.clearSelection(); jlist.setSelectedIndex(x);
    • 如果用户在点击JList之前JButton中选择了多行会发生什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    相关资源
    最近更新 更多