JList 在自动滚动和自动选择时变得很糟糕(GUI 问题)
我创建了一个用于检查代理的日志。
此日志向我显示哪个代理已在使用(通过选择项目)。问题是,当日志自动滚动和自动选择时,我会遇到这样的 GUI 问题(有时最糟糕,有时更好):
此外,GUI 修改(添加项目)是从其他线程进行的。
谁能告诉我如何预防?代码是:
class Log extends JScrollPane {
private JList lista;
private DefaultListModel modelListy;
boolean selScroll;
Log(String opis, boolean selScroll) {
super();
modelListy = new DefaultListModel();
lista = new JList(modelListy);
setViewportView(lista);
setBorder(BorderFactory.createTitledBorder(opis));
this.selScroll = selScroll;
setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
Font displayFont = new Font("Tahoma", Font.BOLD, 10);
lista.setFont(displayFont);
}
void clear() {
modelListy.clear();
}
void addItem(Object obj) {
modelListy.addElement(obj);
if(!selScroll) {
lista.ensureIndexIsVisible(modelListy.size()-1);
}
}
void selectItem(int i) {
lista.setSelectedIndex(i);
if(selScroll) {
lista.ensureIndexIsVisible(i+1);
}
}
String getSelectedItem() {
return (String)lista.getSelectedValue();
}
int itemCount() {
return modelListy.size()-1;
}
int getSelectedIndex() {
return lista.getSelectedIndex();
}
}
I've created a log for checking proxys.
This log shows me, which proxy is already in use (by selecting item). And the problem is that when log is auto scrolling, and auto selecting, im getting GUI issues like this (sometimes worst, sometimes better):
In addition, GUI modifications (adding items) are doing from other thread.
Anyone could tell me how to prevent it? The code is:
class Log extends JScrollPane {
private JList lista;
private DefaultListModel modelListy;
boolean selScroll;
Log(String opis, boolean selScroll) {
super();
modelListy = new DefaultListModel();
lista = new JList(modelListy);
setViewportView(lista);
setBorder(BorderFactory.createTitledBorder(opis));
this.selScroll = selScroll;
setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
Font displayFont = new Font("Tahoma", Font.BOLD, 10);
lista.setFont(displayFont);
}
void clear() {
modelListy.clear();
}
void addItem(Object obj) {
modelListy.addElement(obj);
if(!selScroll) {
lista.ensureIndexIsVisible(modelListy.size()-1);
}
}
void selectItem(int i) {
lista.setSelectedIndex(i);
if(selScroll) {
lista.ensureIndexIsVisible(i+1);
}
}
String getSelectedItem() {
return (String)lista.getSelectedValue();
}
int itemCount() {
return modelListy.size()-1;
}
int getSelectedIndex() {
return lista.getSelectedIndex();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试调用
SwingUtilities.invokeLater()
内的所有lista.ensureIndexIsVisible(...);
。调用重绘时模型更新可能尚未完成。Try to call all the
lista.ensureIndexIsVisible(...);
insideSwingUtilities.invokeLater()
. It's possible that model updating wasn't finished when repaint is called.