I'm trying to write some client/server application using swing. I have a problem with threads communication. I can't understand how to send some data from EDT back to running thread.
class SwingUI extends JFrame implements IUpdater{
@Override
public void askPermission(String str) {
int i = JOptionPane.showConfirmDialog(this, str);
// (?)Send i back
}
}
class Connection implements Runnable{
IUpdater updater;
Connection(IUpdater u){updater = u;}
@Override
public void run() {
String command = "download_file"; //=getCommand(); from a socket
if(command.equals("download_file")){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
updater.askPermition(command);
}
});
// (?)wait until user answers
// int answ = getAnswer();
// if answ == OK
System.out.println("Sending_File");
// if answ == DENY
//System.out.println("Denied");
}
}
}
interface IUpdater{
void askPermission(String str);
}
I need to get somehow response from userUI. Maybe not just int value but List object.
E.g after connection I need to prepare some data for certain client in userForm and then send it. What should I do?
Swingworker allows only to publish data to EDT or fire events which EDT's listening, not to receive messages.
Also I have to make running thread wait somehow. I'm not sure that sleep(n) until some variable changes is a good idea but I do not know how to use monitors in this case and should I. Maybe there are some design patterns solving it?
I'm sure that it is a common situation but I couldn't find any information about it.
Aucun commentaire:
Enregistrer un commentaire