2

I'm trying to get the WindowOpened event from JDialog, but it is fired just once. Why windowClosing works correctly and WindowOpened just once? Is there any way to fire the open event for JDialog every time?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

public class NewClass extends JDialog {

    public void init() {
        setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        setModal(true);
        setSize(100,100);
        setLocationRelativeTo(null);
    }

    public void addListener() {
        addWindowListener(
            new java.awt.event.WindowAdapter() {
            public void windowOpened(WindowEvent e) {
                System.out.println("Invoking WindowOpened from JDialog");
            }
            public void windowClosing(WindowEvent e) {
                System.out.println("Invoking WindowClosing from JDialog");
                dispose();
            }
        });
    }

    public static void main( String args[]) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(200,70);

        final NewClass d = new NewClass();
        d.init();
        d.addListener();

        JButton b = new JButton("Show Dialog");
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                d.setVisible(true);
            }
        });

        f.getContentPane().add(b);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Have you tried WindowActivated? WindowOpened works when it's opened for the first time.. WindowActivated works everytime your window is activated. And change your dialog to JFrame, this way you can minimize your window. – Gabriel Câmara Feb 28 '13 at 16:43
  • 1
    @GabrielCâmara *"And change your dialog to JFrame.."* This is not a good move. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Feb 28 '13 at 16:45
  • I agree @AndrewThompson, but he didn't say that he was using multiple windows. There's nothing wrong in having a frame... It could also be a Main Frame. – Gabriel Câmara Feb 28 '13 at 16:49
  • @GabrielCâmara The code already has one frame. Also the dialog is modal. A second frame instead of the dialog would be a nightmare. BTW - your answer is essentially correct. I especially like the use of `@Override` as a sanity check on the method signature. Why did you delete it? – Andrew Thompson Feb 28 '13 at 17:07
  • You're right about the JFrame that he created.. I didn't read the entire code, my mistake... I deleted because it was unecessary, as long as I could post as a comment. – Gabriel Câmara Feb 28 '13 at 17:20
  • 2
    Thanks Gabriel for your answer. I made a mountain out of a molehill. – Sonny Benavides Feb 28 '13 at 21:32
  • +1 for posting an SSCCE. Glad you got the problem solved with the advice of Gabriel. :) – Andrew Thompson Mar 01 '13 at 02:12
  • Ok, Andrew, you got the point. :D Thanks for yours efforts to teach :D – Gabriel Câmara Mar 01 '13 at 10:41

2 Answers2

3

ComponentListener#componentShown(ComponentEvent e) is fired whenever your Window is made visible.

I don't believe WindowActivated is a good choice (like others said) because it can be fired in some others circumstances. For instance, if your Dialog is not modal, WindowActivated will be fired whenever the window regains focus.

Wez
  • 10,555
  • 5
  • 49
  • 63
2
addWindowListener(new WindowAdapter() {
            @Override
            public void windowActivated(WindowEvent e) {
                // TODO Auto-generated method stub
                super.windowActivated(e);
            }
        });
Gabriel Câmara
  • 1,249
  • 15
  • 22