Results 1 to 5 of 5
  1. #1
    flaquitqm is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default don´t close a jFrame

    Hi everyone:

    I know that this isn´t a new thread but I already search and I can´t solve my problem:

    I want to ask the user if he wants to close the window, the problem is that if he answer No, the windows get close anyway. This is my code, in case anyone can help me:

    Java Code:
    private void initialize() {
    this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    		this.setJMenuBar(getJJMenuBar());
    		this.setContentPane(getJContentPane());
    		this.setTitle("Módulo Administrativo");
    		this.addWindowListener(new java.awt.event.WindowAdapter() {
    			public void windowClosing(java.awt.event.WindowEvent e) {
    				System.out.println("windowClosing()"); // TODO Auto-generated Event stub windowClosing()
    				CerrarConfirmacion(e);
    			}
    		});
    		this.setLocationRelativeTo(null);
    		this.setExtendedState(MAXIMIZED_BOTH);
    		
    	}
    	
    	public void CerrarConfirmacion(WindowEvent e){
    		int respuesta = JOptionPane.showConfirmDialog(null, "¿Está seguro que desea salir de la aplicación?", "Saliendo....", JOptionPane.YES_NO_OPTION);
    		if (respuesta == JOptionPane.YES_OPTION) {
    			System.exit(0);
    		}
    
    	}
    Moderator Edit: code tags added
    Last edited by Fubarable; 04-24-2010 at 08:19 PM. Reason: Moderator Edit: code tags added

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

    Default

    I don't see off hand what you're doing wrong. If you place System.out.println(...) in your CerrarConfirmación method, something like,

    Java Code:
    System.out.println("respuest es " + respuesta);
    does the state of its variable (respuest) make sense?

    Also, please read the link in my signature about using code tags to help your posted code retain its formatting and thus be readable.

    Best of luck!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  4. #4
    flaquitqm is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Unhappy

    Thanks Fubarable for your quick answer. I don´t know what is happening then. The variable takes value 1, so is different and system(0) is skipped, but application is closed, anyway. I'm working with eclipse as IDE, Could be an IDE problem?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The IDE should have nothing to do with this as it is pure Java. I recommend that you create a small program that runs compiles, runs, demonstrates your problem and contains no extraneous code that's unrelated to your problem. For example, something like this:

    Java Code:
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowEvent;
    import javax.swing.*;
    
    public class FlaquitqmFrame extends JFrame {
      public static void main(String[] args) {
        new FlaquitqmFrame().setVisible(true);
      }
      
      public FlaquitqmFrame() {
        initialize();
      }
    
      private void initialize() {
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        // this.setJMenuBar(getJJMenuBar());
        // this.setContentPane(getJContentPane());
        this.setTitle("Módulo Administrativo");
        this.addWindowListener(new java.awt.event.WindowAdapter() {
          public void windowClosing(java.awt.event.WindowEvent e) {
            System.out.println("windowClosing()"); 
            cerrarConfirmacion(e);
          }
        });
        JButton cerrarBtn = new JButton("Cerrar");
        cerrarBtn.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            WindowEvent windowClosing = new WindowEvent(FlaquitqmFrame.this, WindowEvent.WINDOW_CLOSING);
            Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowClosing);
          }
        });
        JPanel panel = new JPanel();
        panel.add(cerrarBtn);
        getContentPane().add(panel);
    
        setPreferredSize(new Dimension(400, 300));
        pack();
        this.setLocationRelativeTo(null);
        // this.setExtendedState(MAXIMIZED_BOTH);
    
      }
    
      public void cerrarConfirmacion(WindowEvent e) {
        int respuesta = JOptionPane.showConfirmDialog(null, 
            "¿Está seguro que desea salir de la aplicación?", "Saliendo....",
            JOptionPane.YES_NO_OPTION);
        if (respuesta == JOptionPane.YES_OPTION) {
          System.exit(0);
        }
    
      }
    }

Similar Threads

  1. Replies: 13
    Last Post: 04-09-2010, 11:15 AM
  2. Can't close JFrame
    By dunafrothint in forum AWT / Swing
    Replies: 5
    Last Post: 12-16-2009, 05:00 PM
  3. JFrame Conditional Close
    By hemanthjava in forum AWT / Swing
    Replies: 10
    Last Post: 06-29-2008, 07:49 AM
  4. How to close an open JFrame window from a jsp page?
    By kasisaiganesh in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-27-2008, 06:29 PM
  5. How to close a JFrame
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:33 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
  •