개인적으로 모든 클라이언트를 중앙 리포지토리에 대조하는 일종의 "클라이언트 관리자"가 있습니다. 이것은 서버 내의 싱글 톤일 것입니다. 현재 활성 사용자 (및 기타 관리 기능)의 목록을 보려면 언제든지 쿼리 할 수 있지만 활성화 된 사용자는 한 번만 있어야합니다.
관리자는 통지 패턴을 사용하여 이해 관계자에게 통지 이벤트를 발생시킵니다.
이러한 당사자 중 하나가 사용자의 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);
}
}
}
}
}
먼저 [Concurrency in Swing] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)을 읽으십시오. – MadProgrammer
스윙에서 스레드 사용에 대해 알고 있지만 이것은 예비 버전이므로, 스레드를 올바르게 사용하지 못했습니다. 나는 그것이 권장되지 않는다는 것을 알고 있지만 그것이 작동한다는 것을 보여줄 필요가있다. 그런 다음 문제를 해결할 것이다. –
그래서, 너는 무엇이 필요 할까? 이벤트 모델? 'DefaultListModel # addElement'? – MadProgrammer