1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| // appeler le fichier Exemple066_FrameLancantUneJDialogModaleUniquementParRapportAElle.java
import java.awt.event.*;
import javax.swing.*;
// Nicolas_75, 30 août 2006
// JFrame lançant une boîte de dialogue non modale,
// mais lui redonnant le focus dès qu'elle le récupère
class JFrameLauchingModalJDialog extends JFrame implements WindowFocusListener {
// Jdialog associée :
private JDialog dialog;
// si la JFrame récupère le focus, elle le transmet à la JDialog :
public void windowGainedFocus(WindowEvent e) {
if (!(dialog==null)) dialog.requestFocus();
}
public void windowLostFocus(WindowEvent e) {}
// quand la JDialog existe, la JFrame est (partiellement) stérilisée :
// pas de redimensionnement ou de fermeture
public void unactivate() {
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
// quand la JDialog est fermée, la JFrame reprend ses
// caractéristiques normales : redimensionnable et fermable
public void reactivate() {
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
// constructeur :
public JFrameLauchingModalJDialog() {
this.setTitle("frame2, associée à une Jdialog");
this.setSize(300,300);
this.setLocation(100,100);
this.setVisible(true);
this.addWindowFocusListener(this);
this.unactivate();
dialog = new LaunchedJDialog(this); //
}
}
// JDialog associée à la JFrame
class LaunchedJDialog extends JDialog {
// JFrame appelante :
final private JFrameLauchingModalJDialog masterFrame;
// constructeur :
public LaunchedJDialog(JFrameLauchingModalJDialog f) {
super(f,false); // false car non modale
masterFrame = f;
this.setTitle("JDialog associée à frame2");
this.setSize(250,250);
this.setLocation(300,300);
// dès que cette JDialog est fermée, elle réactive la JFrame appelante :
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
masterFrame.reactivate();
}
});
this.setVisible(true);
}
}
// "main" de démonstration :
public class Exemple066_FrameLancantUneJDialogModaleUniquementParRapportAElle {
public static void main(String[] args) {
JFrame frame1 = new JFrame();
frame1.setTitle("frame seule - la fermer termine l'application");
frame1.setSize(400,300);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
new JFrameLauchingModalJDialog();
}
} |
Partager