Results 1 to 5 of 5
Thread: don´t close a jFrame
- 04-24-2010, 07:44 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
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:
Moderator Edit: code tags addedJava 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); } }Last edited by Fubarable; 04-24-2010 at 08:19 PM. Reason: Moderator Edit: code tags added
-
I don't see off hand what you're doing wrong. If you place System.out.println(...) in your CerrarConfirmación method, something like,
does the state of its variable (respuest) make sense?Java Code:System.out.println("respuest es " + respuesta);
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!
-
Yep, testing your code confirms that it works, that the problem is not in the code that you posted.
- 04-25-2010, 02:43 AM #4
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
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?
-
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
-
Will the connection.close() and statement.close() ever be called???
By Stephen Douglas in forum New To JavaReplies: 13Last Post: 04-09-2010, 11:15 AM -
Can't close JFrame
By dunafrothint in forum AWT / SwingReplies: 5Last Post: 12-16-2009, 05:00 PM -
JFrame Conditional Close
By hemanthjava in forum AWT / SwingReplies: 10Last Post: 06-29-2008, 07:49 AM -
How to close an open JFrame window from a jsp page?
By kasisaiganesh in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 05-27-2008, 06:29 PM -
How to close a JFrame
By valery in forum New To JavaReplies: 1Last Post: 08-06-2007, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks