0

I'm pretty new to Java and I've tried looking this up but couldn't really find any thing that helped. I'm wondering how I could detect when a specific window is closed? I've been using windowClosing() but this applies to any window that is closed, and so the event that I would want to happen as a result of window A closing also happens if window B closes. How do I go about detecting only if window A is closed? Sorry if this is worded badly, I don't know that much Java terminology. Thanks in advance :)

package gui_login;

//awt classes
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

//swing classes
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.JButton;

public class windowtest implements ActionListener, WindowListener
{
//get JFrame
    JFrame JFrame = new JFrame();

    public static JFrame frameA = new JFrame("FrameA");
    public static JFrame frameB = new JFrame("FrameB");
    static windowtest windowtest = new windowtest();

public static void main(String[] args) 
{
    windowtest.frames();
}

public void frames()
{
    frameA.setLayout(new FlowLayout());
    frameA.setSize(220, 130);
    frameA.setVisible(true);
    frameA.addWindowListener(this);

    frameB.setLayout(new FlowLayout());
    frameB.setSize(220, 130);
    frameB.setVisible(true);
    frameB.addWindowListener(this);
}

public void windowClosing(WindowEvent e) {
    System.out.println("Yo");
}
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
public void actionPerformed(ActionEvent e) {
}
}
  • 1
    Add a `WindowListener` to `A` – MadProgrammer Jun 18 '15 at 03:34
  • [How to Write Window Listeners](https://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html) – MadProgrammer Jun 18 '15 at 03:45
  • Hi, thanks for the reply, that is basically what I have been using, but the windowclosing() event happens no matter which window I close, because I have a windowlistener on both a and b – Aaron Barnard Jun 18 '15 at 04:01
  • So, use a unquie instance for both (so each window get's it's own listener), otherwise Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jun 18 '15 at 04:05
  • Okay so I got an example ready, how do I post it? – Aaron Barnard Jun 18 '15 at 04:31
  • Edit your question and post it there – MadProgrammer Jun 18 '15 at 04:35
  • Okay I have done that thanks :), so as you can see, I have no idea how to create a 'unique instance' for both like you said was needed, and no matter which window is closed, "Yo" is printed out on the console. How do I go about changing this? – Aaron Barnard Jun 18 '15 at 04:44

1 Answers1

3

Because both frameA and frameB are using the same instance of WindowListener (this), when either frame is closed, your WindowListener is notified (of both/either).

You could use WindowEvent#getSource to get the source of the event, but a easier solution might be to give each frame their own instance of a WindowListener, this way, you don't need to make assumptions about what's going

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class WindowTest {
//get JFrame

    JFrame JFrame = new JFrame();

    public static JFrame frameA = new JFrame("FrameA");
    public static JFrame frameB = new JFrame("FrameB");
    static WindowTest windowtest = new WindowTest();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                windowtest.frames();
            }
        });
    }

    public void frames() {
        frameA.setLayout(new FlowLayout());
        frameA.setSize(220, 130);
        frameA.setVisible(true);
        frameA.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("A is closing");
            }

            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("A has closed");
            }

        });

        frameB.setLayout(new FlowLayout());
        frameB.setSize(220, 130);
        frameB.setVisible(true);
        frameB.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("B is closing");
            }

            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("B has closed");
            }

        });
    }
}

You may not have seen anonymous classes before, take a look at Anonymous Classes

Generally speaking, it's no longer required that you implement interfaces which don't provide direct functionality to your class (like you have with ActionListener and WindowListener), the reasoning is, the methods which these interfaces require you to implement are public, but no one should really be calling them directly.

This allows you to "hide" the implementation details internally to your class and prevents possible mis-use by unknown callers

You may also want to take a look at Nested Classes

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366