Bonjour,
Je suis en train de réaliser une application qui se compose d'une JFrame et d'un JMenu. C'est la base de l'appli.
Lorsque j'appelle un JPanel (se trouvant dans une autre classe ) dans ma Frame, l'option du menu qui l'appelle est toujours active, et l'utilisateur peut ouvrir autant de JPanel qu'il veut.
Comment bloquer les éléments du menu qui se trouvent sur la classe mère depuis la classe fille, ou comment désactiver l'accès à ce menu tant que je me trouve dans le JPanel?
Bonne soirée...
------------------------------------------------------------------
Principal.class
---------------------------------------------------------------------
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
84
85
86 import java.awt.Toolkit; import java.awt.event.*; import java.text.ParseException; import javax.swing.*; public class Principal extends JFrame implements ActionListener{ private Toolkit t = Toolkit.getDefaultToolkit(); private JMenuBar barre = new JMenuBar(); public JMenuItem genrapp = new JMenuItem("Génerer rapport autosurveillance"); private JMenuItem ajoutBase = new JMenuItem("Gérer base"); private JMenuItem fonct2 = new JMenuItem("fonctions 2"); private JMenuItem support = new JMenuItem("Support"); private JMenuItem aPropos = new JMenuItem("A propos"); private JMenu rapport = new JMenu("Rapport"); private JMenu menuAutres = new JMenu("Fichier"); private JMenu aide = new JMenu("Aide"); public Principal(){ super("Utilitaire d'exploitation de résultat CANOE"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//fermeture menuAutres.add(ajoutBase); menuAutres.add(fonct2); rapport.add(genrapp); aide.add(support); aide.add(aPropos); barre.add(menuAutres); barre.add(rapport); barre.add(aide); setJMenuBar(barre); this.setSize(t.getScreenSize()); setVisible(true); genrapp.addActionListener(this); ajoutBase.addActionListener(this); } public void actionPerformed(ActionEvent evt) { System.out.print("ok dac"); String selection=evt.getActionCommand(); // Création booléen de condition. if (selection=="Gérer base") { try { GestionBase charg = new GestionBase(this, true); } catch (Exception e) { e.printStackTrace(); } } if (selection=="Génerer rapport autosurveillance") { SelectDate date; try { date = new SelectDate(); add(date); setVisible(true); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { Principal f1 = new Principal(); } }
SelectDate.class
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.*; import javax.swing.text.MaskFormatter; public class SelectDate extends JPanel implements ActionListener{ private JButton valider = new JButton("Valider"); private JButton annuler = new JButton("Annuler"); private DateFormat date_format = new SimpleDateFormat("dd/MM/yyyy"); private MaskFormatter mf = new MaskFormatter("##/##/####"); private JFormattedTextField date_av = new JFormattedTextField(mf); private JFormattedTextField date_ap = new JFormattedTextField(mf); private JLabel lblAv = new JLabel("Date début"); private JLabel lblAp = new JLabel("Date fin"); public SelectDate() throws ParseException{ setLayout(null); date_av.setValue(date_format.format(new Date())); date_ap.setValue(date_format.format(new Date())); lblAv.setBounds(50, 25, 80, 10); lblAp.setBounds(150, 25, 80, 10); date_av.setBounds(50, 50, 80, 20); date_ap.setBounds(150, 50, 80, 20); valider.setBounds(50, 90, 90, 30); annuler.setBounds(150, 90, 90, 30); add(lblAv); add(lblAp); add(date_av); add(date_ap); add(valider); add(annuler); valider.addActionListener(this); annuler.addActionListener(this); setVisible(true); } public void actionPerformed(ActionEvent evt) { String selection=evt.getActionCommand(); // Création booléen de condition. if (selection=="Valider") { boolean verif=true; date_av.setForeground(Color.black); date_ap.setForeground(Color.black); try { String t = date_format.format(date_format.parse(date_av.getText())); String t2 = date_format.format(date_format.parse(date_ap.getText())); if(t.compareTo(date_av.getText()) != 0){ date_av.setForeground(Color.red); date_av.requestFocus(); verif=false; } if(t2.compareTo(date_ap.getText()) != 0){ date_ap.setForeground(Color.red); date_ap.requestFocus(); verif=false; } if(date_format.parse(date_av.getText()).compareTo(date_format.parse(date_ap.getText()))!=-1 | date_format.parse(date_ap.getText()).compareTo(date_format.parse(date_av.getText()))!=1 ){ date_av.setForeground(Color.red); date_av.requestFocus(); verif=false; } if(date_format.parse(date_av.getText()).compareTo(date_format.parse(date_ap.getText()))== 0 | date_format.parse(date_ap.getText()).compareTo(new Date()) !=-1 ){ date_ap.setForeground(Color.red); date_ap.requestFocus(); } } catch (Exception e) { System.out.println("Exception"); } } if(selection=="Annuler"){ System.out.print("ok"); setVisible(false); } } }
Partager