在选择0索引的情况下删除和添加时,Swing JList冻结 [英] Swing JList freezes when I delete and add while 0 index was selected

查看:59
本文介绍了在选择0索引的情况下删除和添加时,Swing JList冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个示例,其中您按下按钮,并且jList1重新填充了从a1到a1000的项目.:

This is an example where you press a button and jList1 is refilled with items from a1 to a1000.:

//variable
private List<String> list = new ArrayList<>();

...

//main method
jList1.setModel(new DefaultListModel());
for(int i = 0; i < 1000; i++) {
    list.add("a"+i);
}

...

//button action - jList1 refill
DefaultListModel dtm = (DefaultListModel)jList1.getModel();
dtm.removeAllElements();
for(String s : list) {
    dtm.addElement(s);
}

如果我填写了jList1,则选择(用鼠标)0索引(jList中的第一个元素),然后按一下按钮,程序将在重新填充列表时冻结.如果我选择任何其他元素,或者根本不选择列表中的任何项目,那么它就可以满足要求.

If I fill the jList1, then select (with mouse) 0 index (first element in jList) and then press the button the program freezes while refilling the list. If I select any other element or do not select any item in the list at all then it fills just fine.

P.S.此示例是在没有任何摆动或EWT线程的情况下完成的,因为主要的原因是发现使用了它们.

P.S. This example is done without any swing or EWT threads, because the main reason was found using them.

SSCCE:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lt;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

/**
 *
 * @author Minutis
 */
public class Window {

    public static void main(String[] args) {
        final List<String> list = new ArrayList<>();


        JFrame frame = new JFrame("BorderLayout Frame");
        JPanel panel = new JPanel();
        final JList jList1 = new JList();
        JButton refill = new JButton("Refill");

        jList1.setModel(new DefaultListModel());
        for(int i = 0; i < 1000; i++) {
            list.add("a"+i);
        }

        refill.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DefaultListModel dtm = (DefaultListModel)jList1.getModel();
                dtm.removeAllElements();
                for(String s : list) {
                    dtm.addElement(s);
                }
            }

        });

        frame.add(panel);
        panel.setLayout(new BorderLayout());
        panel.add(jList1, BorderLayout.CENTER);
        panel.add(refill, BorderLayout.SOUTH);
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

推荐答案

问题是由在选择第一个元素时删除模型中的所有元素引起的.当索引被选择并被删除时,由于某种原因,它会生成一个selectionChanged事件,它的实现方式.如果选择了另一个索引而又删除了另一个索引(即使这意味着元素移位),则不会生成selectionChanged事件.

The problem is cause by removing all elements in the model when the first element is selected. The way it is implemented, when an index is selected and is removed, for some reason it generates a selectionChanged event. If another index is selected while another is deleted (even if it meant element shifting), it doesn't generate a selectionChanged event.

删除的方式是,它会定期删除第一个元素,而选择索引保持不变.因此,对于选择索引0的情况,它将生成1000个selectionChanged事件,必须由EDT和侦听器处理.如果选择另一个索引,则仅生成1个事件.这明显减少了开销.

The removal is implemented in a way that it periodically removes the first element, while the selection index remains unchanged. So for the case of index 0 being selected, it generates 1000 selectionChanged events, which must be handled by the EDT and listeners. If you select another index, only 1 event is generated. That is significantly less overhead.

在选择索引0时重新填充之前,请尝试手动将选择设置为索引1:

Before the repopulating while index 0 is selected, try manually setting the selection to index 1:

if(jList1.getSelectedIndex() == 0){
     jList1.setSelectedIndex(1);
}

我认为,如果列表中只有一个元素,则无需担心-它不应做任何事情来设置比该元素数更大的索引.但这可能是特定于实现的.

I think there is no need to worry if there is only one element in the list - it shouldn't do anything to set index greater that element count. But that may be implementation-specific.

由于某些原因,clearSelection()将导致为重新填充空列表而生成selectionChanged事件.

For some reason, clearSelection() will cause selectionChanged events to be generated for repopulating an empty list.

这篇关于在选择0索引的情况下删除和添加时,Swing JList冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆