Bonjour,

J'ai actuellement une appli qui permet l'ouverture/fermeture de certains ports de pare-feu.
3 classes : Connexion.java qui gère les connexion Telnet, Controleur.java qui gère les fonctionnalités (ouverture,fermeture,gestion de fichier log, actualisation...) et Vue.java qui gère l'affichage des éléments (labels, boutons, voyant d'état de connexion).
D'abord, je n'utilisais pas de Layout et "empilait" mes composant les uns à la suite des autres, en faisant attention (via la taille de la fenêtre et le fait qu'elle soit non-resizable) à ce que chaque nouveau label change de ligne ; problème, en fonction de la taille des labels, tout n'était pas forcément aligné :

Nom : W1DNTp.jpg
Affichages : 219
Taille : 40,3 Ko

J'ai donc opté pour l'utilisation d'un BorderLayout pour le JPanel principal, insérer des JPanel dans chacune des partie du BorderLayout (pour plus de flexibilité sur la taille des composants à mettre dedans) et enfin utiliser un GridLayout pour les JPanel des parties WEST, CENTER et EAST qui vont contenir mes labels, boutons et voyants de connexion.
Je suis donc arrivé au résultat que je désirai :

Nom : R8Lv4E.jpg
Affichages : 196
Taille : 37,2 Ko

Seulement voilà, depuis, j'ai des gros bug dans mon affichage lorsque j'actualise mon voyant d'état de connexion ; c'est à dire lorsque je clique sur un bouton pour changer l'état ou quand je clique sur le bouton d'actualisation [Note : je n'ai rien modifié dans ma class Controleur.java] :

Clic sur bouton radio pour changer d'état
Nom : 9NVca8.jpg
Affichages : 192
Taille : 39,9 Ko
On peut voir au niveau de l'état modifié qu'un bouton radio avec le label "Fermé" apparait sous mon voyant d'état

Clic sur le bouton d'actualisation (de tous les boutons et voyants)
Nom : 3UrvA7.jpg
Affichages : 184
Taille : 41,0 Ko
Là c'est l'anarchie : des boutons radio et actualiser qui s'affiche un peu partout sous mes voyants


Remarque : Si je rezise la fenêtre ou que je la diminue dans la barre de tâche pour la réagrandir : ces bugs d'affichage disparaissent...

Voici mes 2 classes, avec en gras, les parties qui concernent l'actualisation des voyants d'état :

Vue.java
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
public class Vue extends JFrame {

	private static final long serialVersionUID = 1L;
	Controleur firewall = new Controleur();	
	JPanel panel = new JPanel();
	JPanel paneln = new JPanel();
	JPanel panels = new JPanel();
	JPanel panelw = new JPanel();
	JPanel panelc = new JPanel();
	JPanel panele = new JPanel();
	JLabel image = new JLabel(new ImageIcon(this.getClass().getResource("logoVPN.png")));
	JButton refresh = new JButton("Actualiser");
	
	public Vue(){
		Color background = new Color(255,228,202);
		int labelMax = 0; // JLabel le + long pour adaptabilité de la largeur de l'appli
		int nbrAcces = 0; // Nombre d'accès pour adaptabilité de la hauteur de l'appli
		panel.setLayout(new BorderLayout(6,12));
		panel.add(paneln, BorderLayout.NORTH);
		panel.add(panelw, BorderLayout.WEST);
		panel.add(panelc, BorderLayout.CENTER);
		panel.add(panele, BorderLayout.EAST);
		panel.add(panels, BorderLayout.SOUTH);
		paneln.add(image);
		panelw.setLayout(new GridLayout(firewall.getListAcces().size(),1,0,3));
		panelc.setLayout(new GridLayout(firewall.getListAcces().size(),1,0,3));
		panele.setLayout(new GridLayout(firewall.getListAcces().size(),1,0,3));
		for(int j=0 ; j<firewall.getListAcces().size() ; j++){
			Controleur.Acces acces = firewall.getListAcces().get(j);
			boolean etatConnexion = firewall.getEtatAcces(acces);
			panelw.add(new JLabel(acces.getLabel(),javax.swing.SwingConstants.CENTER));
			for(int i=0 ; i<acces.getBoutons().length ; i++){
				acces.getBoutons()[i].addActionListener(new GestionActionListener(firewall,acces,acces.getBoutons()[i].getText()));
				panelc.add(acces.getBoutons()[i]);
			}
			panele.add(acces.getEtat());
			firewall.actualiserEtat(acces,etatConnexion);
			firewall.actualiserBoutons(acces,etatConnexion);
			if(acces.getLabel().length()>labelMax){
				labelMax = acces.getLabel().length();
			}
			nbrAcces++;
		}
		refresh.addActionListener(new GestionActionRefreshListener(firewall));
		panels.add(refresh);
		/*if(firewall.getModeDefault()){

		}*/
		panelw.setPreferredSize(new Dimension(labelMax*7,300));
		panel.setBackground(background);
		paneln.setBackground(background);
		panels.setBackground(background);
		panele.setBackground(background);
		panelw.setBackground(background);
		panelc.setBackground(background);
		this.setTitle("Gestion Accès VPN");
		this.setSize(190+(labelMax*7),125+(nbrAcces*31));
		//this.setResizable(false);
		this.addWindowListener(new GestionWindowListener(firewall));
		this.setLocationRelativeTo(null);
		this.setContentPane(panel);
		this.setVisible(true);
	}
		
	public static void main(String[] args){
		new Vue();
	}
}
Controleur.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
public class Controleur {

	private Connexion telnet;
	private Connexion telnet2;
	private String cheminLog;
	private String cheminConf;
	private List<Acces> listAcces;
	private boolean mode_default;
	
	public Controleur() {
		telnet = new Connexion( "IP_ACCES", "LOGIN", "MDP",'PROMPT');
		telnet2 = new Connexion( "IP_FIREWALL", "LOGIN", "MDP", 'PROMPT1', 'PROMPT2');
		cheminLog = "CHEMIN_FICHIER_LOG";
		cheminConf = "CHEMIN_FICHIER_CONF";
		mode_default = false;
		try {
			listAcces = getConfAcces();
		} catch (IOException e) {
			listAcces = getDefaultAcces();
			mode_default = true;
			e.printStackTrace();
		}
	}
	
	public boolean getModeDefault(){
		return this.mode_default;
	}
	
	public String getCheminLog(){
		return this.cheminLog;
	}
	
	public List<Acces> getListAcces(){
		return this.listAcces;
	}
		
	public class Acces{

		private String label ="";
		private String connexion ="";
		private EtatVPN etat;
		private String[] ports;
		private JRadioButton[] boutons;
		private ButtonGroup groupeBoutons;
		
		public Acces(String l, String type_connexion, String... num_ports){
			this.label = l;
			this.etat = new EtatVPN();
			this.connexion = type_connexion;
			this.ports = num_ports;
			this.boutons = new JRadioButton[2];
			this.boutons[0]= new JRadioButton("Ouvert");
			this.boutons[1]= new JRadioButton("Fermé");
			this.boutons[0].setBackground(new Color(255,228,202));
			this.boutons[1].setBackground(new Color(255,228,202));
			this.groupeBoutons =new ButtonGroup();
			this.groupeBoutons.add(this.boutons[0]);
			this.groupeBoutons.add(this.boutons[1]);
		}
		
		public EtatVPN getEtat(){
			return this.etat;
		}
		
		public String getLabel(){
			return label;
		}
		
		public String getConnexion(){
			return connexion;
		}
		
		public String[] getPorts(){
			return ports;
		}
		
		public JRadioButton[] getBoutons(){
			return boutons;
		}
	}
	
	private List<Acces> getDefaultAcces(){
		List<Acces> listeAcc = new ArrayList<Acces>();
		listeAcc.add(new Acces("Acces1" , "ACCES" , "14","17"));
		listeAcc.add(new Acces("Acces2" , "ACCES" , "16","19"));
		listeAcc.add(new Acces("Acces3" , "ACCES" , "23"));
		listeAcc.add(new Acces("Acces4" , "ACCES" , "24"));
		listeAcc.add(new Acces("FirewallA" , "FIREWALL" , "26"));
		listeAcc.add(new Acces("FirewallB" , "FIREWALL" , "33"));
		listeAcc.add(new Acces("FirewallC" , "FIREWALL" , "25"));

		return listeAcc;
	}
	
	private List<Acces> getConfAcces() throws IOException{
		List<Acces> listeAcc = new ArrayList<Acces>();
		ArrayList<String> tmp = null;
		String line = "";
		BufferedReader bis = new BufferedReader(new FileReader(this.cheminConf));
		while((line = bis.readLine()) != null){
			tmp = new ArrayList<String>();
			for(String s : line.split(",")){
				tmp.add(s);
			}
			String[] lineTab = new String[tmp.size()];
			lineTab = tmp.toArray(lineTab);
			String[] portsTab = new String[lineTab.length-2];
			for(int i=2 ; i<lineTab.length ; i++){
				portsTab[i-2]=lineTab[i];
			}
			Acces acc = new Acces(lineTab[0],lineTab[1],portsTab);
			listeAcc.add(acc);
		}
		bis.close();
		return listeAcc;
	}
	
	private void envoiTelnetCommande(Acces acc, String statut){
		switch(acc.getConnexion()){
		case "ACCES":
			for(int i=0 ; i<acc.getPorts().length ; i++){
				telnet.sendCommand( "config firewall policy");
				telnet.sendCommand( "edit "+acc.getPorts()[i]);
				telnet.sendCommand( "set status "+statut);
				telnet.sendCommand( "end");
			}
			break;
		case "FIREWALL":
			for(int i=0 ; i<acc.getPorts().length ; i++){
				telnet2.sendCommand2( "configure terminal");
				telnet2.sendCommand2( "interface "+acc.getPorts()[i]);
				telnet2.sendCommand2( statut);
				telnet2.sendCommand2( "exit");
				telnet2.sendCommand2( "exit");
			}
			break;
		}
	}
	
	public boolean getEtatAcces(Acces acc){		
		switch(acc.getConnexion()){
		case "ACCES":
			for(int i=0 ; i<acc.getPorts().length ; i++){
				String s = telnet.sendCommand( "show firewall policy "+ acc.getPorts()[i]);
				if(s.indexOf("disable")!= -1){
					return false;
				}
			}
			return true;
		case "FIREWALL":
			for(int i=0 ; i<acc.getPorts().length ; i++){
				String s = telnet.sendCommand( "show interface "+ acc.getPorts()[i] + " status");
				if(s.indexOf("disable")!= -1){
					return false;
				}
			}
			return true;
		}
		return false;
	}
	
	public void disconnect(){
		telnet.disconnect();
		telnet2.disconnect();
	}	
	
	public void ouvrirAcces(Acces acc){
		String statut = "";
		String connexion = acc.getConnexion();;
		switch(connexion){
		case "ACCES":
			statut = "enable";
			envoiTelnetCommande(acc,statut);break;
		case "FIREWALL":
			statut = "no shutdown";
			envoiTelnetCommande(acc,statut);break;
		}
	}
	
	public void fermerAcces(Acces acc){
		String statut = "";
		String connexion = acc.getConnexion();
		switch(connexion){
		case "ACCES":
			statut = "disable";
			envoiTelnetCommande(acc,statut);break;
		case "FIREWALL":
			statut = "shutdown";
			envoiTelnetCommande(acc,statut);break;
		}
	}
	
	public void actualiserEtat(Acces acc, boolean etatConnexion){
		acc.etat.setEtat(etatConnexion);acc.etat.repaint();
	}
	
	public void actualiserBoutons(Acces acc, boolean etatConnexion){
		acc.boutons[0].setSelected(etatConnexion == acc.boutons[0].getText().equalsIgnoreCase("Ouvert"));
		acc.boutons[1].setSelected(etatConnexion == acc.boutons[1].getText().equalsIgnoreCase("Ouvert"));
	}
}



class GestionActionListener implements ActionListener{
	
	private Controleur firewall;
	private Controleur.Acces acces;
	private String textBouton;
	
	public GestionActionListener(Controleur gaf, Controleur.Acces gafa, String s){
		firewall = gaf;
		acces = gafa;
		textBouton = s;
	}
	
	public void actionPerformed(ActionEvent e) {
		switch(textBouton){
		case "Ouvert" : firewall.ouvrirAcces(acces);
						firewall.actualiserEtat(acces,true);
						try {
							ecrireLog();
						} catch (IOException e1) {
							e1.printStackTrace();
						}
						break;
		case "Fermé" :  firewall.fermerAcces(acces);
						firewall.actualiserEtat(acces,false);
						try {
							ecrireLog(
									);
						} catch (IOException e1) {
							e1.printStackTrace();
						}
						break;
		}
	}
	
	public void ecrireLog() throws IOException{
			Calendar c = Calendar.getInstance();
			DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
			String ip;
			try {
				ip = InetAddress.getLocalHost().getHostAddress();
			} catch (UnknownHostException e) {
				ip = "IP INCONNUE";
			}
			String acc = acces.getLabel();
			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(firewall.getCheminLog() , true)));
			pw.println(ip+" - "+df.format(c.getTime())+" - "+acc+" "+textBouton);
			pw.close();
	}
}



class GestionActionRefreshListener implements ActionListener{
	
	Controleur firewall;
	
	public GestionActionRefreshListener(Controleur gaf){
		firewall = gaf;
	}
	
	public void actionPerformed(ActionEvent e) {
		for(int i=0 ; i<firewall.getListAcces().size() ; i++){
			boolean etatConnexion = firewall.getEtatAcces(firewall.getListAcces().get(i));
			firewall.actualiserEtat(firewall.getListAcces().get(i),etatConnexion);
			firewall.actualiserBoutons(firewall.getListAcces().get(i), etatConnexion);
		}
	}
}



class GestionWindowListener implements WindowListener {	
	
	Controleur firewall;
	
	public GestionWindowListener(Controleur gaf){
		firewall = gaf;
	}
	
	public void windowActivated(WindowEvent arg0) {}		
	public void windowClosed(WindowEvent arg0) {}
	public void windowClosing(WindowEvent arg0) {
		firewall.disconnect();
	}
	public void windowDeactivated(WindowEvent arg0){}
	public void windowDeiconified(WindowEvent arg0){}		
	public void windowIconified(WindowEvent arg0){}			
	public void windowOpened(WindowEvent arg0){}	
}



class EtatVPN extends JPanel{

	private static final long serialVersionUID = 1L;
	private boolean etatAcces;	

	public void paintComponent(Graphics g){
		g.setColor(Color.white);
		g.fillRect(5, 5, this.getWidth()/2, this.getHeight()/2);
		if(etatAcces){
			g.setColor(Color.GREEN);
		}else{
			g.setColor(Color.RED);
		}
		g.fillRect(5, 5, this.getWidth()/2, this.getHeight()/2);;
	}
	
	public Dimension getPreferredSize(){
		return new Dimension(30,30);
	}
	
	public void setEtat(boolean b){
		etatAcces = b;
	}		
}
En espérant que vous puissiez trouver une solution à mon problème... (sans ça, adieu mes Layout !)
Merci d'avance !!