gpt4 book ai didi

Java JList 模型 addElement() 破坏了列表的视觉表示

转载 作者:行者123 更新时间:2023-12-01 11:06:09 26 4
gpt4 key购买 nike

向 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
}

现已添加完整的示例代码。给您带来不便,敬请谅解!

最佳答案

This one is inner class of the frame used to create new threads receiving data and adding string to JList.

Swing 组件需要在事件调度线程上更新,因此您无法直接在线程中更新组件。

因此,您可以将更新模型的代码包装在 SwingUtilities.invokeLater(...)

或者,您可以使用 SwingWorker 并在结果可用时“发布”结果,而不是使用单独的线程。

阅读 Swing 教程中关于 Concurrency 的部分有关这些概念和工作示例的更多信息。

关于Java JList 模型 addElement() 破坏了列表的视觉表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32928315/

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