我读了很多关于 Swing 的文章,但我已经走进了死胡同,我知道你可以帮助我。 我读过很多问题,例如 Updating an JList但我仍然不知道如何继续。 我的问题和提出我提到的问题的人是一样的。我正在制作一个服务器,用户将访问它。 这是我的类(class)。
服务器
private string name;
private string dateOfAccess;
@Override
public String toString() {
// TODO Auto-generated method stub
return nombreAmigo;
}
主要
private DefaultListModel listModel = new DefaultListModel();
private JList list=new JList(listModel);
和我的 ClientHandler
public static List<Conexion> clientes=new ArrayList<Conexion>();
因此,当客户端连接到我的服务器时,我将从不同的线程填充客户端列表,并且我需要在我的 Jlist 中显示它们。关于如何更新有什么建议吗?我真的被困在这里了 谢谢!
最佳答案
就我个人而言,我会拥有某种“客户经理”,负责将所有客户整理到一个集中存储库中。这将是您服务器中的单例。可以随时查询当前 Activity 用户的列表(以及其他管理功能),但应该只有一个 Activity 用户。
然后,经理将向感兴趣的各方发出通知事件(使用观察模式)。
其中之一就是您的用户界面。当引发“连接”或“断开连接”事件时,您需要确保在尝试更新列表模型之前将其同步回事件调度线程,例如...
public void userConnected(UserManagerEvent evt) { // You would need to define all this yourself...
final Conexion user = evt.getConnection(); // You would need to define this event yourself...
EventQueue.invokeLater(new Runnable() {
public void run() {
listModel.addElement(user);
}
});
}
实际的实现将取决于你想要实现什么以及你想要实现它的方式,但这是基本概念......
更新了概念示例
这是一个基本的概念性示例。按钮引发事件,模拟(例如)连接。然后,该事件通过监听器接口(interface)发送到列表,模型将在其中更新
事件是从其他来源生成的,并且 UI 在事件发生时更新,经典的观察者模式
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.w3c.dom.ls.LSInput;
public class UpdateListOnEvent {
public static void main(String[] args) {
new UpdateListOnEvent();
}
public UpdateListOnEvent() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ConnectionEvent {
private Date date;
public ConnectionEvent(Date date) {
this.date = date;
}
public Date getDate() {
return date;
}
}
public interface ConnectionListener {
public void connectionEstablished(ConnectionEvent evt);
}
public class TestPane extends JPanel implements ConnectionListener {
private JList list;
private DefaultListModel<String> model;
public TestPane() {
setLayout(new BorderLayout());
model = new DefaultListModel<>();
list = new JList(model);
add(new JScrollPane(list));
EventPane eventPane = new EventPane();
eventPane.addConnectionListener(this);
add(eventPane, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
@Override
public void connectionEstablished(ConnectionEvent evt) {
model.addElement(DateFormat.getDateTimeInstance().format(evt.getDate()));
}
}
public class EventPane extends JPanel {
private List<ConnectionListener> listeners;
private JButton update;
public EventPane() {
listeners = new ArrayList<>(5);
setLayout(new GridBagLayout());
update = new JButton("Update");
update.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// connectionEstablished(new Date());
fireConnectionEstablished(new Date());
}
});
add(update);
}
public void addConnectionListener(ConnectionListener listener) {
listeners.add(listener);
}
public void removeConnectionListener(ConnectionListener listener) {
listeners.remove(listener);
}
protected ConnectionListener[] getConnectionListeners() {
return listeners.toArray(new ConnectionListener[listeners.size()]);
}
protected void fireConnectionEstablished(Date date) {
ConnectionListener[] listeners = getConnectionListeners();
if (listeners != null && listeners.length > 0) {
ConnectionEvent evt = new ConnectionEvent(date);
for (ConnectionListener listener : listeners) {
listener.connectionEstablished(evt);
}
}
}
}
}
关于java - 动态更新Jlist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16329037/