У меня есть поток SwingWorker, который я использую для обновления пользовательского интерфейса JLabel, и помимо методов publish()/process() программа работает (в том смысле, что JLabel успешно публикуется с соответствующим текстом/фоном/границей и т. д. .). Однако я хочу использовать метод process(), чтобы установить для текста JLabel значение «Connecting...», в то время как doInBackground() выполняет свою работу, но метод process() в моей программе никогда не вызывается (я, очевидно, с помощью метода публикации()). Предложения?
Вот SwingWorker:
public class PcclientBackgroundWork extends SwingWorker < String, String> {
public JLabel connectionStatus2;
String msg;
/* Constructor */
public PcclientBackgroundWork(JLabel label){
connectionStatus2 = label;
}
/*Background work to determine Application connection status
and pass corresponding message (String msg) to done() */
@Override
public String doInBackground() throws Exception {
String serverName = "localhost"; //UDP is sent within same machine
int port = 6789;
try {
Socket client = new Socket(serverName, port);
BufferedReader in = new BufferedReader
(new InputStreamReader(client.getInputStream()));
while(!in.ready()){
publish("Connecting"); //Want this method called until the bufferedReader is ready.
} //Loops until ready
msg = in.readLine(); //Incoming text is only one line
if(msg.equals("Connection Unsuccessful"))
{
msg = "Application Connection Failed";
} else {
msg = "App Connected " + msg;
}
System.out.println("msg = " + msg);
in.close(); //Close reader
client.close(); //Close client socket
} catch (IOException e) {
e.printStackTrace();
msg = "Application Connection Failed"; //JLabel text set the same as
} //if connection is unsuccessful (lines 66-68)
return msg;
}
public void process(String msg){
System.out.println("process method called...");
connectionStatus2.setText(msg);
}
/*Method to set JLabel information when doInBackground() is complete */
@Override
public void done() {
try {
connectionStatus2.setText(get()); //get() is the return value of doInBackground (String msg)
connectionStatus2.setBorder(BorderFactory.createLineBorder(Color.black));
connectionStatus2.setVisible(true);
connectionStatus2.setOpaque(true);
if(get().equals("Application Connection Failed")){
connectionStatus2.setBackground(Color.PINK);
} else {
connectionStatus2.setBackground(Color.GREEN);
}
} catch (InterruptedException ex) {
} catch (ExecutionException ex) {
Logger.getLogger(PcclientUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Я не публиковал поток пользовательского интерфейса, потому что функции SwingWorker мне нравятся, кроме методов публикации/обработки. Заранее спасибо!