目标:
用于任何目的。
问题:
如果选择了第一个条目,然后您清除模型并添加新内容,则无论您多久做一次,这都会非常慢。只有选择了另一个条目 - 不是第一个 - 行为将是快速=正常的。
解决方案:
每次清除模型之前,都要编程添加两个任意条目(两个,因为列表可能完全是空的(,然后选择具有最高有效索引的条目。
问题:
您可以复制该错误吗?它是已知的错误吗?如果没有,请有人发布它,以便openjdk修复?
测试环境:
标准Oracle Java版本下载JDK8202。
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 Home,版本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&quot" 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();
}
}
(标题编辑:删除&quot,绝对是Java错误&quot&quot&quot&quot
问题在某种程度上是值列表的原型单元格值,因为我现在将解决方案缩减为一行:设置此值:
valueList.setPrototypeCellValue(" ");
再次,我不知道为什么会起作用,但是问题必须驻留在这里,因为这解决了一切。也许这与列表如何与JSCrollpane进行交互有关,因为原型单元格值是计算可滚动视口大小和首选大小的关键。
我还不知道问题的根源(因此,这是作为"社区Wiki"的回答,而不是个人答案(,但是如果您的问题消失了,则该问题消失了设置Jlist的可见行计数和原型单元格值(这是秋千最佳实践(。
请注意,在更改模型之前删除值列表的选择侦听器,然后在更改模型后重新添加它们,这无助于您的问题。我的直觉是这是一个渲染和尺寸的问题。
工作代码:
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();
}
}
每次执行此操作:
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));
}
这使模型将一个listDataevent发送给JLIST,而不是每个元素的事件,因此Jlist仅重新绘制一次。
更新:
在Java 8中,您想通过简单地创建一个新的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);