2

我已经阅读了很多关于 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 中显示它们。关于如何更新它的任何建议?我真的被困在这里谢谢!

4

1 回答 1

5

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

然后,经理将向感兴趣的各方发出通知事件(使用观察模式)。

其中之一就是您的 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);
                }
            }
        }

    }
}
于 2013-05-02T01:49:25.223 回答