gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:09:28 27 4
gpt4 key购买 nike

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

问题:
如果选择了第一个条目,然后清除模型并添加新内容,这将非常慢,无论您​​这样做的频率如何。只有在您选择了另一个条目后 - 不是第一个 - 行为将再次变为 fast=normal。

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

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

测试环境:
标准Oracle Java版本下载JDK8 202.
Java 版本“1.8.0_202”
Java(TM) SE 运行时环境(build 1.8.0_202-b08)
Java HotSpot(TM) 64 位服务器 VM(构建 25.202-b08,混合模式)
Windows 10 家庭版,版本 1809(操作系统内部版本 17763.437)
Intel(R) Core(TM) i7-6700K CPU @ 4.00 GHz 4.01 GHz

也用 OpenJDK 11
转载openjdk 版本“11.0.2”2019-01-15
OpenJDK 运行时环境 18.9(build 11.0.2+9)
OpenJDK 64 位服务器 VM 18.9(build 11.0.2+9,混合模式)

中南合作中心

此代码最少,因此它甚至使用默认的 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 错误”)

最佳答案

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

valueList.setPrototypeCellValue("                ");

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

关于java - 如果选择第一个条目 (Windows 10),Swing JList 会非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55936230/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com