我在SwingWorker中有一个ServerSocketChannel连接进程。在Swing应用程序本身中,两个JLabels应该更新为(1)一个字符串(连接状态)和(2)一个int (连接的客户端数)。下面是"Detect Clients“JButton运行连接过程之前的应用程序屏幕截图。但是,我不确定如何发布()和处理()以便在EDT上更新多个Swing组件。有谁有关于如何实现这一点的指导吗?

因为List<V>是process()的参数,所以我尝试将<Object>作为<V>。然而,这似乎遇到了从String/int到对象的转换问题,反之亦然。
下面的演示代码说明了应该发布更新的几个要点:
protected Void doInBackground() {
try {
// Omitted: obtain selector
ServerSocketChannel ssc = ServerSocketChannel.open() // could fail, may need
// to publish status
ssc.socket().bind(serverAddress); // could fail, may need to publish status
ssc.configureBlocking(false); // could fail, may need to publish status
// Omitted: register ssc
while (true) {
int count = sel.select(1000); // may need to publish for # of clients
// Omitted: rest of processing
}
} catch (IOException e) {
//handle error
}
}发布于 2012-09-16 00:45:16
你可以使用字符串作为结果类型。假设对于一个整数(客户机数)执行publish(String.valueOf(integer)),然后在您的process()方法中尝试如下所示
try {
int numClients = Integer.parseInt(passedString);
//all well, it's the client number
} catch(NumberFormatException ex) {
//not a number, must be a connection status
}或者简单地使用setNumClients, setConnStatus和适当的getter将这两个信息包装在一个单独的类中,在执行过程中填充它,并发布该类的对象。
如果我没有完全理解您的意思,请告诉我,因为我有一种预感,您可能想让PropertyChangeListeners参与进来。
https://stackoverflow.com/questions/12439005
复制相似问题