Java JList 模型 addElement() 破坏了列表的视觉表示
Java JList Model addElement() breaks visual representation of the list
将元素添加到 JList 模型时会出现奇怪的行为。在调用 addElement() 方法时,列表变为空白,改为添加巨大的空元素或将新元素向下移动几行。对这些方法的另一个调用将它返回到问题前后的所有元素,包括 "unstable" 调用添加的项目。
起初它似乎是绘画问题,只是重绘调用没有帮助,只有添加新元素才能解决问题。
问题发生在添加部分元素时,相同的索引涉及不同的启动。从未见过它很快就会在第一个索引上失败。
package r;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
public class R extends javax.swing.JFrame {
ServerSocket serverport;
/**
* Creates new form R
*/
public R() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
scrollDebug = new javax.swing.JScrollPane();
debugList = new javax.swing.JList();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Debug");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
scrollDebug.setAutoscrolls(true);
debugList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
scrollDebug.setViewportView(debugList);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scrollDebug, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scrollDebug, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void formWindowOpened(java.awt.event.WindowEvent evt) {
new Thread() {
@Override
public void run() {
try {
serverport = new ServerSocket(33002, 0, InetAddress.getLoopbackAddress());
debugList.setModel(new DefaultListModel());
while (R.this.isVisible()) {
new ClientConnection(serverport.accept()).start();
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error opening server port", "Debug", JOptionPane.ERROR_MESSAGE);
}
}
}.start();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(R.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(R.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(R.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(R.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new R().setVisible(true);
}
});
}
class ClientConnection extends Thread {
Socket connection;
public ClientConnection(Socket connection){
this.connection=connection;
}
@Override
public void run() {
try {
connection.getOutputStream().write(String.format("debug>").getBytes());
connection.getOutputStream().flush();
String response = "";
do {
response += (char) connection.getInputStream().read();
} while (!response.endsWith(System.lineSeparator()));
response = response.replace(System.lineSeparator(), "");
((DefaultListModel) debugList.getModel()).addElement(response);
if (((DefaultListModel) debugList.getModel()).getSize() > 0) {
//debugList.ensureIndexIsVisible(((DefaultListModel) debugList.getModel()).getSize() - 1);
debugList.setSelectedIndex(((DefaultListModel) debugList.getModel()).getSize() - 1);
scrollDebug.getVerticalScrollBar().setValue(scrollDebug.getVerticalScrollBar().getMaximum());
}
connection.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error receiving connection", "Debug", JOptionPane.ERROR_MESSAGE);
}
}
}
// Variables declaration - do not modify
private javax.swing.JList debugList;
private javax.swing.JScrollPane scrollDebug;
// End of variables declaration
}
现在添加了完整的示例代码。抱歉给您带来不便!
This one is inner class of the frame used to create new threads receiving data and adding string to JList.
Swing 组件需要在事件调度线程上更新,因此您不能直接在线程中更新组件。
因此您可以将更新模型的代码包装在 SwingUtilities.invokeLater(...)
或者,您可以使用 SwingWorker
和 "publish" 结果,而不是使用单独的线程。
阅读有关 Concurrency 的 Swing 教程部分,了解有关这些概念和工作示例的更多信息。
将元素添加到 JList 模型时会出现奇怪的行为。在调用 addElement() 方法时,列表变为空白,改为添加巨大的空元素或将新元素向下移动几行。对这些方法的另一个调用将它返回到问题前后的所有元素,包括 "unstable" 调用添加的项目。 起初它似乎是绘画问题,只是重绘调用没有帮助,只有添加新元素才能解决问题。
问题发生在添加部分元素时,相同的索引涉及不同的启动。从未见过它很快就会在第一个索引上失败。
package r;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
public class R extends javax.swing.JFrame {
ServerSocket serverport;
/**
* Creates new form R
*/
public R() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
scrollDebug = new javax.swing.JScrollPane();
debugList = new javax.swing.JList();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Debug");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
scrollDebug.setAutoscrolls(true);
debugList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
scrollDebug.setViewportView(debugList);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scrollDebug, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scrollDebug, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void formWindowOpened(java.awt.event.WindowEvent evt) {
new Thread() {
@Override
public void run() {
try {
serverport = new ServerSocket(33002, 0, InetAddress.getLoopbackAddress());
debugList.setModel(new DefaultListModel());
while (R.this.isVisible()) {
new ClientConnection(serverport.accept()).start();
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error opening server port", "Debug", JOptionPane.ERROR_MESSAGE);
}
}
}.start();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(R.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(R.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(R.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(R.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new R().setVisible(true);
}
});
}
class ClientConnection extends Thread {
Socket connection;
public ClientConnection(Socket connection){
this.connection=connection;
}
@Override
public void run() {
try {
connection.getOutputStream().write(String.format("debug>").getBytes());
connection.getOutputStream().flush();
String response = "";
do {
response += (char) connection.getInputStream().read();
} while (!response.endsWith(System.lineSeparator()));
response = response.replace(System.lineSeparator(), "");
((DefaultListModel) debugList.getModel()).addElement(response);
if (((DefaultListModel) debugList.getModel()).getSize() > 0) {
//debugList.ensureIndexIsVisible(((DefaultListModel) debugList.getModel()).getSize() - 1);
debugList.setSelectedIndex(((DefaultListModel) debugList.getModel()).getSize() - 1);
scrollDebug.getVerticalScrollBar().setValue(scrollDebug.getVerticalScrollBar().getMaximum());
}
connection.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error receiving connection", "Debug", JOptionPane.ERROR_MESSAGE);
}
}
}
// Variables declaration - do not modify
private javax.swing.JList debugList;
private javax.swing.JScrollPane scrollDebug;
// End of variables declaration
}
现在添加了完整的示例代码。抱歉给您带来不便!
This one is inner class of the frame used to create new threads receiving data and adding string to JList.
Swing 组件需要在事件调度线程上更新,因此您不能直接在线程中更新组件。
因此您可以将更新模型的代码包装在 SwingUtilities.invokeLater(...)
或者,您可以使用 SwingWorker
和 "publish" 结果,而不是使用单独的线程。
阅读有关 Concurrency 的 Swing 教程部分,了解有关这些概念和工作示例的更多信息。