|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Jan 2004
Città: ROMA
Messaggi: 2055
|
[JAVA] Chiedere conferma su chiusura applicazione alla pressione del tasto X
Come faccio a fare in modo che quando un utente chiude l'applicazione (sto usando un JFrame) utilizzando il tasto X in alto a destra, gli venga chiesta una conferma, con un JOptionPane.showConfirmDialog() ???
|
![]() |
![]() |
![]() |
#2 | |
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
Quote:
Codice:
import java.awt.event.*; import javax.swing.*; public class TestFrame extends JFrame { public TestFrame () { super ("Test Frame"); setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE); setSize (300, 300); addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent e) { int ret = JOptionPane.showConfirmDialog (TestFrame.this, "Close the window?", "Test Frame", JOptionPane.YES_NO_OPTION); if (ret == JOptionPane.YES_OPTION) dispose (); } }); } public static void main (String[] args) { SwingUtilities.invokeLater (new Runnable () { public void run () { TestFrame f = new TestFrame (); f.setVisible (true); } }); } }
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) |
|
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
Il primo passo è impedire alla finestra di chiudersi con:
finestra.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); Poi procedi come per un JButton. Aggiungi alla finestra un ascoltatore di eventi (WindowListener). Ha cinque metodi, quello che riceve una notifica quando l'utente cerchi di chiudere la finestra è "windowClosing". Brevemente: Codice:
finestra.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { confermaChiusura(e.getWindow()); } }); ... private void confermaChiusura(Window w) { int ans = JOptionPane.showConfirmDialog... if(ans == JOptionPane.YES_OPTION) { w.dispose(); } }
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
In questi forum ci vorrebbe un bel semaforone che si illumina di rosso se qualcuno ha già risposto mentre uno sta scrivendo il suo papiro.
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
![]() |
![]() |
![]() |
#5 | |
Senior Member
Iscritto dal: Nov 2005
Città: TO
Messaggi: 5206
|
Quote:
Aggiungo che se uno volesse realizzare una gestione di questo warning in modo più facilmente riutilizzabile (magari per diversi frame), si può fare una classe: Codice:
class CloseWarningListener extends WindowAdapter { private JFrame f; public CloseWarningListener (JFrame f) { this.f = f; } public void windowClosing (WindowEvent e) { int ret = JOptionPane.showConfirmDialog (f, "Close the window?", "Test Frame", JOptionPane.YES_NO_OPTION); if (ret == JOptionPane.YES_OPTION) f.dispose (); } }
__________________
Andrea, SCJP 5 (91%) - SCWCD 5 (94%) |
|
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Jan 2004
Città: ROMA
Messaggi: 2055
|
Grazie ragazzi,
siete stati molto gentili. Quello che mi avete fatto imparare (e ciò di cui avevo bisogno di sapere) è che bisogna dare un "DO_NOTHING_ON_CLOSE" al JFrame e aggiungere un evento del tipo windowClosing(). Non lo sapevo. Grazie EDIT: una cosa, perchè fare dispose() e non System.exit(0); ???? Devo chiudere tutto. Ultima modifica di fbcyborg : 19-04-2007 alle 12:38. Motivo: Aggiunta domanda |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 03:05.