AWT Window Close Listener/Event
Asked Answered
S

2

22

I am sorry if this is a n00b question, but I have spent way too long for this once I create the Window listener, window event, and everything else, how do I specify what method to invoke? Here is my code:

private static void mw() {
    Frame frm = new Frame("Hello Java");
    WindowEvent we = new WindowEvent(frm, WindowEvent.WINDOW_CLOSED);
    WindowListener wl = null;
    wl.windowClosed(we);
    frm.addWindowListener(wl);
    TextField tf = new TextField(80);
    frm.add(tf);
    frm.pack();
    frm.setVisible(true);

}

I am trying to get a URL, and Download it, I have everything else worked out, I am just trying to get the window to close.

Swan answered 16/8, 2011 at 3:41 Comment(10)
Don't you think you should tell us what you're trying to do, what your goal is? Invoke a method to do what?Impertinent
Also, why develop with AWT and not Swing?Impertinent
For GCJ, also makes porting to applet easier (or so I've heard), have no idea, been coding java for less than 2 months.Swan
I don't know "GCJ" but I can tell you that the second statement above is patently false. Edit: do you mean gnu compiler for Java? Why on earth would you use that?Impertinent
I kind of expected that, the GCJ is the Gnuc Compiler for Java, which can produce "Native" executables that do not depend on the JVM/JRE installed on the system to function, it is not complete, and supports only AWT, and some of Swing, it does however, support SWT, which I might use, it will be a learning experience too (Figuring this darn thing out)!Swan
Just to explore my Java cravings man... Just exploring the resources of a language which I have just discovered, and which is very beautiful in its syntax (in my opinion)Swan
I know, and I understand, but the thing is, I simply want to learn about the process, then I can put this AWT & GCJ nonsense behind me, and Andrew, I don't know if you can, but turn your comment into an answer, it's probably the best I'll get. How about this... Forget everything about GCJ, and just answer the part about window listener/events, I am having trouble finding a source, and I spent a long time searchingSwan
And since you mention 'applet' and 'download' note. Only a trusted applet/JWS app. can get data across domains, and it would take either a trusted version of same to save files to the local file system, or being deployed using JWS & using the JNLP API file services.Opsonize
K, I'll have to get to that later, right now I am just exploring as I please, and I will probably not use GCJ, I might go for Excelsior JETSwan
AWT is heavyweight i.e. its components uses the resources of system, Swing provides platform-independent and lightweight components such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etcMasha
O
33

Window closing method

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class FrameByeBye {

    // The method we wish to call on exit.
    public static void showDialog(Component c) {
        JOptionPane.showMessageDialog(c, "Bye Bye!");
    }

    public static void main(String[] args) {
        // creating/udpating Swing GUIs must be done on the EDT.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {

                final JFrame f = new JFrame("Say Bye Bye!");
                // Swing's default behavior for JFrames is to hide them.
                f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                f.addWindowListener( new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent we) {
                        showDialog(f);
                        System.exit(0);
                    }
                } );
                f.setSize(300,200);
                f.setLocationByPlatform(true);
                f.setVisible(true);

            }
        });
    }
}

Also look into Runtime.addShutdownHook(Thread) for any action that is vital to perform before shutting down.

AWT

Here is an AWT version of that code.

import java.awt.*;
import java.awt.event.*;

class FrameByeBye {

    // The method we wish to call on exit.
    public static void showMessage() {
        System.out.println("Bye Bye!");
    }

    public static void main(String[] args) {
        Frame f = new Frame("Say Bye Bye!");
        f.addWindowListener( new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {
                showMessage();
                System.exit(0);
            }
        } );
        f.setSize(300,200);
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }
}
Opsonize answered 16/8, 2011 at 4:26 Comment(4)
Thanks, I am still trying to avoid swing, but I am pretty sure this will work without swing with some tweaking, btw sorry if I sounded like a troll in the comments, or whatever (is troll the right word?)Swan
+1 See also, this related example.Disyllable
Hehe, And with SWT, it makes swing even sexier!! A little less portable though, mebbe' I could automatically download latest platform lib with my awesome java knowledge of almost nothing!Swan
Thank you for going out of your way to do that, It's people like you that make Stack Overflow "not just another Q & A site"Swan
D
5

This example shows how to use addWindowListener() with a WindowAdapter, a concrete implementation of the WindowListener interface. See also, How to Write Window Listeners.

Disyllable answered 16/8, 2011 at 4:14 Comment(3)
Finally! Thank you for your answer, especially by someone as active as you trashgod!Swan
You're welcome; I admire your enthusiasm. I would also endorse the suggestions of Andrew and HFOE, from whom I continue to learn. Java has evolved considerably over the last decade, and one should be wary of inadvertently adopting outdated practices.Disyllable
Agreed, there seems to be a lot more java stuff <2004 that is just plain outdated.Swan

© 2022 - 2024 — McMap. All rights reserved.