如果选择第一个条目 (Windows 10),Swing JList 会非常慢

Swing JList slows down extremely if 1st entry selected (Windows 10)

目标:
将 JList 用于任何目的。

问题:
如果第一个条目是 selected,然后您清除模型并添加新内容,这将 非常 缓慢,无论您​​这样做的频率如何。只有当您 select 编辑了另一个条目 - 不是第一个 - 行为将再次快速=正常。

解决方法:
每次清除模型之​​前,以编程方式添加两个任意条目(两个,因为列表可能完全为空),然后 select 具有最高有效索引的条目。

问题:
你能重现这个错误吗?这是一个已知的错误吗?如果没有,有人可以 post 以便修复 OpenJDK 吗?

测试环境:
标准Oracle Java 发布下载JDK8 202.
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
Windows 10 主页,版本 1809(OS 内部版本 17763.437)
Intel(R) Core(TM) i7-6700K CPU @ 4.00 GHz 4.01 GHz

也用OpenJDK 11
转载 openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

SSCCE

这段代码很少,所以它甚至使用了默认的 LookAndFeel。本来我在使用L&F "Windows".

的时候就遇到了问题
package jlistslowdownbugdemo;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;




final public class Main {


    final private static boolean ENABLE_FIX = false;


    public static void main(final String[] args) {


        SwingUtilities.invokeLater(() -> {


            final DefaultListModel<String> valueListModel = new DefaultListModel<>();
            final JList<String> valueList = new JList<>(valueListModel);


            final DefaultListModel<String> keyListModel = new DefaultListModel<>();
            for (String key : Main.DATA.keySet()) {
                keyListModel.addElement(key);
            }
            final JList<String> keyList = new JList<>(keyListModel);


            keyList.addListSelectionListener(e -> {

                if (e.getValueIsAdjusting()) { // To prevent SUPER-slowdown when the bug kicks. Not an important measure, just for comfortable demoing.


                    // Workaround for the bug: Add two entries (two, because list may be entirely empty),
                    // and select the last available entry (in the worst case, this is the 2nd).
                    // Just so that one is selected that IS NOT THE FIRST.
                    // This problem AND fix occurred in Oracle JDK 8 and Open JDK 11.
                    if (ENABLE_FIX) {
                        valueListModel.addElement(null);
                        valueListModel.addElement(null); // Make sure you add something else instead of null if your GUI/JList setup requires it.
                        valueList.setSelectedIndex(valueListModel.size() - 1);
                    }


                    valueListModel.clear();

                    final String key = keyList.getSelectedValue();
                    if (key != null) {
                        for (String value : Main.DATA.get(key)) {
                            valueListModel.addElement(value);
                        }
                    }

                }
            });


            final JPanel contentPane = new JPanel(new GridLayout(1, 0, 0, 0));
            contentPane.add(new JScrollPane(keyList));
            contentPane.add(new JScrollPane(valueList));


            final JFrame window = new JFrame();
            window.setContentPane(contentPane);
            window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            window.setMinimumSize(new Dimension(1200, 1000));
            window.setLocationRelativeTo(null);
            window.setVisible(true);


            final String msg = "Click&drag in the left list. This rapidly changes the content of the right list.\n" +
                    "\n" +
                    "Now select any entry EXCEPT the first entry of the right list. Click&drag again in the left list,\n" +
                    "it still works just as rapidly. Now click THE FIRST entry of the right list.\n" +
                    "\n" +
                    "If you NOW click&drag again in the left list, you will experience BRUTAL slowdown.\n" +
                    "\n" +
                    "Once Swing has calmed down, click any entry EXCEPT the first entry of the right list.\n" +
                    "Click&drag again in the left list - the problem is gone.\n" +
                    "\n" +
                    "The built-in workaround (off by default) simulates this.";

            JOptionPane.showMessageDialog(window,
                                          msg,
                                          "How to reproduce the Swing bug:",
                                          JOptionPane.INFORMATION_MESSAGE);


        });

    }


    final private static Random RAND = new Random(0);


    final private static Map<String, List<String>> DATA = createDataMap();


    private static Map<String, List<String>> createDataMap() {

        final Map<String, List<String>> ret = new HashMap<>();

        for (int i = 0; i < 30; i++) {


            final int listSize = 20 + RAND.nextInt(5000);


            final String key = generateRandomString('A');
            final List<String> value = new ArrayList<>(listSize);
            ret.put(key, value);


            for (int ii = 0; ii < listSize; ii++) {
                value.add(generateRandomString('a'));
            }

        }

        return ret;
    }


    private static String generateRandomString(final char baseChar) {

        final StringBuilder sb = new StringBuilder();

        final int len = 4 + RAND.nextInt(17);

        for (int i = 0; i < len; i++) {
            sb.append((char) (baseChar + RAND.nextInt(26)));
        }

        return sb.toString();
    }


}

(标题已编辑:删除“,绝对是 Java 错误”)

我还不知道问题的根源(因此这是一个 "community wiki" 的回答,而不是个人回答),但是如果您设置 JList 的可见行,问题就会消失计数和原型单元格值(这是 Swing 最佳实践)。

请注意,在更改模型之前删除值列表的选择侦听器,然后在更改模型后重新添加它们,对您的问题没有帮助。我的直觉是这是渲染和调整大小的问题。

工作代码:

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

final public class Foo {

    public static void main(final String[] args) {
        SwingUtilities.invokeLater(() -> {
            final DefaultListModel<String> valueListModel = new DefaultListModel<>();
            final JList<String> valueList = new JList<>(valueListModel);
            final DefaultListModel<String> keyListModel = new DefaultListModel<>();
            for (String key : Foo.DATA.keySet()) {
                keyListModel.addElement(key);
            }
            final JList<String> keyList = new JList<>(keyListModel);
            keyList.addListSelectionListener(e -> {
                if (e.getValueIsAdjusting()) {
                    valueListModel.clear();
                    final String key = keyList.getSelectedValue();
                    if (key != null) {
                        for (String value : Foo.DATA.get(key)) {
                            valueListModel.addElement(value);
                        }
                    }
                }
            });

            keyList.setVisibleRowCount(25);
            valueList.setVisibleRowCount(25);
            String prototypeFormat = "%100s";
            keyList.setPrototypeCellValue(String.format(prototypeFormat, " "));
            valueList.setPrototypeCellValue(String.format(prototypeFormat, " "));
            final JPanel contentPane = new JPanel(new GridLayout(1, 0, 0, 0));
            contentPane.add(new JScrollPane(keyList));
            contentPane.add(new JScrollPane(valueList));
            final JFrame window = new JFrame();
            window.setContentPane(contentPane);
            window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            // window.setMinimumSize(new Dimension(1200, 1000));
            window.pack();
            window.setLocationRelativeTo(null);
            window.setVisible(true);
        });
    }

    final private static Random RAND = new Random(0);
    final private static Map<String, List<String>> DATA = createDataMap();

    private static Map<String, List<String>> createDataMap() {
        final Map<String, List<String>> ret = new HashMap<>();
        for (int i = 0; i < 30; i++) {
            final int listSize = 20 + RAND.nextInt(5000);
            final String key = generateRandomString('A');
            final List<String> value = new ArrayList<>(listSize);
            ret.put(key, value);
            for (int ii = 0; ii < listSize; ii++) {
                value.add(generateRandomString('a'));
            }
        }
        return ret;
    }

    private static String generateRandomString(final char baseChar) {
        final StringBuilder sb = new StringBuilder();
        final int len = 4 + RAND.nextInt(17);
        for (int i = 0; i < len; i++) {
            sb.append((char) (baseChar + RAND.nextInt(26)));
        }
        return sb.toString();
    }
}

问题出在值列表的原型单元格值上,因为现在我已将解决方案缩减为一行:设置此值:

valueList.setPrototypeCellValue("                ");

同样,我不知道为什么会这样,但问题一定出在这里,因为这可以解决所有问题。也许它与列表如何与 JScrollPane 交互有关,因为原型单元格值是计算可滚动视口大小和首选大小的关键。

每次你执行这个:

valueListModel.addElement(value);

您正在使正确的 JList 重绘自身。这样做几十次是你变慢的原因。

您需要一次更改整个模型。替换为:

final String key = keyList.getSelectedValue();
if (key != null) {
    for (String value : Main.DATA.get(key)) {
        valueListModel.addElement(value);
    }
}

有了这个:

final String key = keyList.getSelectedValue();
if (key != null) {
    valueListModel.addAll(Main.DATA.get(key));
}

这会导致模型向 JList 发送一个 ListDataEvent,而不是为每个元素发送一个事件,因此 JList 只重绘一次。

更新:

在Java8中,你想通过简单地创建一个新的ListModel来实现同样的“一次设置整个模型”的效果。替换为:

valueListModel.clear();

final String key = keyList.getSelectedValue();
if (key != null) {
    for (String value : Main.DATA.get(key)) {
        valueListModel.addElement(value);
    }
}

有了这个:

DefaultListModel<String> newListModel = new DefaultListModel<>();

final String key = keyList.getSelectedValue();
if (key != null) {
    for (String value : JListSlowDown.DATA.get(key)) {
        newListModel.addElement(value);
    }
}

valueList.setModel(newListModel);