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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
alexmherrmann
  • 1,055
  • 2
  • 9
  • 17
  • 1
    Don't you think you should tell us what you're trying to do, what your goal is? Invoke a method to do ***what***? – Hovercraft Full Of Eels Aug 16 '11 at 03:43
  • 1
    Also, why develop with AWT and not Swing? – Hovercraft Full Of Eels Aug 16 '11 at 03:43
  • For GCJ, also makes porting to applet easier (or so I've heard), have no idea, been coding java for less than 2 months. – alexmherrmann Aug 16 '11 at 03:45
  • 2
    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? – Hovercraft Full Of Eels Aug 16 '11 at 03:50
  • 1
    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)! – alexmherrmann Aug 16 '11 at 03:52
  • 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) – alexmherrmann Aug 16 '11 at 03:54
  • 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 searching – alexmherrmann Aug 16 '11 at 04:13
  • 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. – Andrew Thompson Aug 16 '11 at 04:13
  • 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 JET – alexmherrmann Aug 16 '11 at 04:15
  • 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 etc – Jaimin Patel Mar 04 '16 at 10:44

2 Answers2

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);
    }
}
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 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?) – alexmherrmann Aug 16 '11 at 04:29
  • 1
    +1 See also, this related [example](http://stackoverflow.com/questions/6163606/does-awt-swing-cancel-painting-operations-if-the-monitor-is-off/6165657#6165657). – trashgod Aug 16 '11 at 04:36
  • 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! – alexmherrmann Aug 16 '11 at 04:40
  • 1
    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" – alexmherrmann Aug 16 '11 at 04:56
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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Finally! Thank you for your answer, especially by someone as active as you trashgod! – alexmherrmann Aug 16 '11 at 04:16
  • 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. – trashgod Aug 16 '11 at 04:29
  • Agreed, there seems to be a lot more java stuff <2004 that is just plain outdated. – alexmherrmann Aug 16 '11 at 04:32