只有向JList模型添加元素纔會有奇怪的行爲。在調用addElement()方法時,列表變爲空白,添加巨大的空元素或將新元素向下移動幾行。對於這些方法的另一個要求是在問題出現之前和之後使用所有元素,包括通過「unstable」調用添加的項目。 首先它似乎是繪畫問題,只是重繪調用不起作用,只有添加新元素才能糾正問題。Java JList模型addElement()打破列表的可視化表示
這個問題發生,然後添加一部分元素,不同的啓動涉及相同的索引。從來沒有見過它在第一個索引很快失敗。
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
}
現在添加完整的示例代碼。對造成的不便表示歉意!
認真嗎?當我們沒有可用的信息時,你如何期待我們提供幫助?我們不知道要添加到ListModel或其他任何細節的元素是什麼。發佈一個合適的[SSCCE](http://sscce.org/)來說明問題。 – camickr
這不是SSCCE!請解釋我們如何編譯和執行該代碼? – camickr