Can you help me out here? A really simple problem but I just can't get what the solution is!
I am coding a listener application that runs on its own thread & listens on a ServerSocket for incoming connections.
When a connection arrives, a new 'Message' object is created on a new thread and passed the incoming text data "messageData
". This object should then display the text to the user in a popup window.
In Listener.java:
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Message.display(messageData);
}
});
Then, in Message.java:
public Message {
public static void display(String data) {
Message message = new Message(data);
message.initGUI();
}
String messageData;
GUI gui;
public Message(String data) {
messageData = data;
gui = new GUI();
}
public void initGUI() {
gui.init();
// add listeners
gui.addOKListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// close GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui.close();
}
});
gui = null;
}
});
}
public class GUI {
do GUI stuff
... initialise gui
... add components
... setVisible=true
}
}
The idea is that the inner class GUI is the 'View' and that the Message object is the model and controller combined.
Each Message object initially creates itself via the static Message.display()
method. This means that there can be unlimited Message objects all existing independently and the Listener doesn't have to worry about managing them.
Great. However I now have a problem.
When the user clicks 'OK' on the GUI, I want (a) the GUI to be deleted/hidden, AND (b) the Message object to be deleted from memory.
How can I do (a) and (b)? I can do (a) by
gui=null
in gui.addOKListener()
(see above) but I can't do (b) because I don't have a reference to the Message object.
I have tried to define addOKListener() in Message.display()
but this doesn't work because I have to make the message
object that I create final, therefore message=null
is illegal.
A really simple problem- I am guessing the solution is also simple. This is very frustrating..
Thanks alot
Will