Results 1 to 4 of 4
  1. #1
    AndyV is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default windowClosed(WindowEvent e) occuring twice

    Hi everybody!
    I have a custom JDialog for user to input information. This information is stored in parent frame when the dialog is closed (this works fine). The problem is that windowClosed(WindowEvent e) event occurs two times, so i get double entry from dialogs return value.

    I've searched the web, but haven't found any solutions so far. Can anyone explain why this event occurs two times, and how to fix it?

    Here's sample code:

    Java Code:
    package gui;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class SimpleFrame extends JFrame {
    
        public SimpleFrame() {
    	super("Simple Frame");
    	this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    	this.getContentPane().setLayout(new BorderLayout());
    	this.setPreferredSize(new Dimension(600, 400));
    
    	JButton openButton = new JButton("Open Dialog");
    	openButton.addActionListener(new ActionListener() {
    	    public void actionPerformed(ActionEvent e) {
    		
    		final AddBookDialog dialog = new AddBookDialog(null);
    
    		dialog.addWindowListener(new WindowAdapter() {
    		    public void windowClosed(WindowEvent e) {
    
    			System.out.println("event: " + e);
    
    		    }
    		});
    	    }
    	});
    	
    	this.getContentPane().add(openButton, BorderLayout.PAGE_START);
    	this.pack();
    	this.setVisible(true);
        }
    
        public static void main(String[] args) {
    	new SimpleFrame();
        }
    }
    package gui;
    
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    
    public class AddBookDialog extends JDialog {
    
        private JButton cancelButton;
        private final JDialog thisDialog = this;
    
        public AddBookDialog(JFrame parent) {
    	super(parent, "Add new item", true);
    	this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	Container container = this.getContentPane();
    	container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
    	Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    	this.setBounds((int) ((screen.getWidth() - 200) / 2),
    		(int) ((screen.getHeight() - 200) / 2), 400, 300);
    
    	this.setMinimumSize(new Dimension(400, 220));
    	this.setMaximumSize(new Dimension(400, 220));
    	this.setResizable(false);
    
    
    	cancelButton = new JButton("Cancel");
    	cancelButton.addActionListener(new ActionListener() {
    	    public void actionPerformed(ActionEvent e) {
    		thisDialog.setVisible(false);
    		thisDialog.dispose();
    	    }
    	});
    
    	Box buttonBox = Box.createHorizontalBox();
    	buttonBox.createHorizontalGlue();
    	buttonBox.add(cancelButton);
    	container.add(buttonBox);
    
    	this.pack();
    	this.setVisible(true);
        }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure why this occurs, but the problem goes away if you give your dialog a JFrame parent, the SimpleFrame JFrame:

    Java Code:
    final AddBookDialog dialog = new AddBookDialog(SimpleFrame.this);

    But another question is why even bother with a WindowListener? The dialog is modal, and so you will know exactly when it closes without need for a WindowListener.

  3. #3
    AndyV is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks a lot, the problem was missing parent in JDialogs constructor :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're welcome, but again and regardless, you're better off without the WindowListener.

Similar Threads

  1. windowClosed() vs windowClosing()
    By Lil_Aziz1 in forum New To Java
    Replies: 6
    Last Post: 08-05-2010, 05:49 AM
  2. HELP ON errors occuring in the code
    By jaiminparikh in forum Advanced Java
    Replies: 6
    Last Post: 02-12-2009, 03:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •