开发者

Java: Deleting a GUI object from within the GUI

开发者 https://www.devze.com 2022-12-19 06:07 出处:网络
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 incomi

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 its开发者_如何转开发elf 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


If all you want to do is to display a dialog with a short test message to the user it should be a lot easier if you used JOptionPane and one of the static factory methods in there.

Have a look at the tutorial


You cannot force objects to be deleted from memory, but the dispose method may be what you are looking for to remove the GUI:

// close GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {

    public void run() {
        gui.dispose();
    }
});

This is assuming that your GUI class extends JDialog or JFrame


If you have a popup, then you have a frame somewhere. Frames are top-level objects which exist even if no one keeps a reference to them (well, the VM probably does but you don't have to).

So the solution is to wait for the click on the OK or close button of the popup and then just dispose it. Don't keep references to the popup anywhere and the VM will clean up for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消