有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java动态更新Jlist

我在Swing上读了很多书,但我遇到了死胡同,我知道你可以帮我。 我读过很多像Updating an 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中显示他们。对如何更新它有什么建议吗?我真的被困在这里了 谢谢


共 (1) 个答案

  1. # 1 楼答案

    就我个人而言,我会有某种“客户经理”,负责将所有客户整理成一个集中的存储库。这将是服务器中的一个单例。可以随时查询当前活动用户(和其他管理功能)的列表,但应该只有一个活动用户

    然后,经理将向相关方发出通知事件(使用观察模式)

    其中之一就是你的用户界面。当引发“连接”或“断开连接”事件时,您需要确保在尝试更新列表模型之前将其同步回事件调度线程,例如

    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);
            }
        });
    }
    

    实际的实现将取决于你想要实现什么,以及你想要实现它的方式,但这是基本概念

    更新了概念示例

    这是一个基本的、概念性的例子。按钮会引发一个事件,该按钮模拟(例如)一个连接。然后通过侦听器接口将此事件发送到列表,并在其中更新模型

    enter image description here

    事件是从其他来源生成的,当它们发生时,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);
                    }
                }
            }
    
        }
    }