[英]Dynamically updating Jlist
我已经在Swing上阅读了很多东西,但是我走到了穷途末路,我知道您可以帮助我。 我已经阅读了许多诸如更新JList之类的问题,但对于如何进行我仍然一无所知。 我的问题和问我提到的问题的人是一样的。 我正在制作服务器,用户将可以访问它。 这是我的课。
服务器
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中显示它们。 关于如何更新的任何建议? 我真的被困在这里,谢谢!
就个人而言,我将拥有某种“客户管理器”,负责将所有客户整理到一个集中的存储库中。 这将是服务器内的一个单例。 可以随时查询当前活动用户(和其他管理功能)的列表,但是应该永远只有一个活动用户。
然后,经理将通知事件发送给感兴趣的各方(使用观察模式)。
这些参与方之一就是您的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);
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.