View Full Version : [JAVA] Chiedere conferma su chiusura applicazione alla pressione del tasto X
fbcyborg
19-04-2007, 11:47
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() ???
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() ???Ecco:
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);
}
});
}
}
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:
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();
}
}
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.
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.Nessun problema, figurati.
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:
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 ();
}
}Da usare poi in modo semplice con: addWindowListener (new CloseWarningListener (this));
fbcyborg
19-04-2007, 12:35
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.
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.