就个人而言,我会有某种“客户经理”,负责将所有客户整理到一个集中的存储库中。这将是您服务器中的单例。可以随时查询当前活动用户(和其他管理功能)的列表,但应该只有一个活动用户。
然后,经理将向感兴趣的各方发出通知事件(使用观察模式)。
其中之一就是您的 UI。当引发“连接”或“断开连接”事件时,您需要确保在尝试更新列表模型之前将其同步回事件调度线程,例如......
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);
}
});
}
实际的实现将归结为您想要实现的目标以及您想要实现它的方式,但这是基本概念......
更新了概念示例
这是一个基本的、概念性的示例。按钮引发事件,该按钮模拟(例如)连接。然后通过侦听器接口将此事件发送到列表,在该列表中更新模型
事件是从其他来源生成的,当它们发生时 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);
}
}
}
}
}