Salut à tous,
je suis entrain de travailler sur une petite application avec swing.
je cherche à gérer les événements de mon application. en cherchant sur le web j'ai trouvé l'article suivant Simplifier l'écriture des listeners Java avec EventHandler sur developpez.net
j'ai essayé d'appliquer ce que me convient dans mon apllication mais rien ne fonctionne. Alors le voilà ce que j'ai fait.
la classe ToolBarPane: c'est la classe dont je construit un toolbar personnalisé
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
package com.jam.views;
 
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Rectangle;
 
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.jam.business.CallPanesService;
import com.jam.templates.BeanFactoryTemp;
import com.jam.templates.ItemPane;
 
import java.awt.ComponentOrientation;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.EventHandler;
 
 
@SuppressWarnings("serial")
public class ToolBarPane extends JPanel{
 
	private ItemPane openItem = null;
	private ItemPane trsItem = null;
	private ItemPane importItem = null;
	private ItemPane exportItem = null;
 
	ContainerPane cp = new ContainerPane(983, 584);
 
	public ToolBarPane(int w, int h){
 
		setSize(w, h);
		initialize();
	}
	/**
         * This method initializes this
         * 
         */
	private void initialize(){
 
		setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		add(getOpenItem(), FlowLayout.LEFT);
		add(getAddTaskItem(), FlowLayout.LEFT);
		add(getStructureItem(), FlowLayout.LEFT);
		add(getTrsItem(), FlowLayout.LEFT);
		add(getImportItem(), FlowLayout.LEFT);		
		add(getExportItem(), FlowLayout.LEFT);
		add(getEmailItem(), FlowLayout.LEFT);
		add(getAddChatItem(), FlowLayout.LEFT);
		add(getDiaryItem(), FlowLayout.LEFT);
		add(getCalendarItem(), FlowLayout.LEFT);
		add(getAlertItem(), FlowLayout.LEFT);
		add(getNoteBookItem(), FlowLayout.LEFT);
		add(getCalculatorItem(), FlowLayout.LEFT);
		add(getWebSiteItem(), FlowLayout.LEFT);
		add(getSearchItem(), FlowLayout.LEFT);
		add(getStatisticItem(), FlowLayout.LEFT);
		add(getConfigItem(), FlowLayout.LEFT);
 
	}
 
	/**
         * This method initializes openItem     
         *      
         * @return javax.swing.JPanel   
         */
	public ItemPane getOpenItem() {
		if (openItem == null) {
			openItem = new ItemPane("assets/new.png", "Nouveau");
			openItem.setName("openItem");
			openItem.addMouseListener(EventHandler.create(MouseListener.class, cp, "addSMPane"));
		}
		return openItem;
	}
 
	/**
         * This method initializes trsItem      
         *      
         * @return javax.swing.JPanel   
         */
	private ItemPane getTrsItem() {
		if (trsItem == null) {
			trsItem = new ItemPane("assets/budget.png","Encaissements" );
		}
		return trsItem;
	}
 
	/**
         * This method initializes importItem   
         *      
         * @return javax.swing.JPanel   
         */
	private ItemPane getImportItem() {
		if (importItem == null) {
			importItem = new ItemPane("assets/import.png", "Import");
			importItem.setSize(70, 50);
		}
		return importItem;
	}
 
	/**
         * This method initializes exportItem   
         *      
         * @return javax.swing.JPanel   
         */
	private ItemPane getExportItem() {
		if (exportItem == null) {
			exportItem = new ItemPane("assets/export.png", "Export");
		}
		return exportItem;
	}
 
 
}
Lorsque je clique sur un bouton du toolbar je veux apparaitre l'interface qui convient à ce bouton qui est définit dans une autre classe ContainerPane.
c'est dans cette classe et via cette méthode que je veux switcher entre les interfaces à afficher lorsque je clique sur un bouton du toolbar.
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
package com.jam.views;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
 
import javax.swing.JLabel;
import javax.swing.JPanel;
 
import com.jam.business.CallPanesService;
import com.jam.templates.BeanFactoryTemp;
import com.jam.templates.OvalPanel;
 
public class ContainerPane extends OvalPanel{
 
	private OvalPanel welcomePane = null;
	private OvalPanel switchPane  =  null;
	private JLabel welcomeLbl = null;
	SyndicatManagerPane smp = null;
 
	public ContainerPane(){}
 
	public ContainerPane(int w, int h) {
		super(w, h);
		//initialize();
 
	}
	public ContainerPane(int w, int h, Dimension d) {
		super(w, h, d);
		initialize();
	}
 
	/**
         * This method initializes this
         * 
         */
	public void initialize() {
        setLayout(new BorderLayout());
        add(getWelcomePane(), BorderLayout.NORTH);
        add(getSwitchPane(), BorderLayout.CENTER);	
 
	}
 
 
	/**
         * This method initializes welcomeLbl   
         *      
         * @return javax.swing.JPanel   
         */
	private JLabel getWelcomeLbl() {
		if (welcomeLbl == null) {
			welcomeLbl = new JLabel("Bienvenue au Syndicat ");
			welcomeLbl.setFont(new Font("Calibri", Font.PLAIN, 13));
 
		}
		return welcomeLbl;
	}
 
	public void addSMPane(){
		smp = new SyndicatManagerPane(getSwitchPane().getWidth(), getSwitchPane().getHeight()); 
		getSwitchPane().add(smp);
	}
	/**
         * This method initializes menuPane     
         *      
         * @return javax.swing.JPanel   
         */
	public OvalPanel getSwitchPane() {
		if (switchPane == null) {
			switchPane =  new OvalPanel(10, 10, new Dimension(this.getWidth(), this.getHeight()));
 
		}
		return switchPane;
	}
 
 
	/**
         * This method initializes syndicatView 
         *      
         * @return javax.swing.JPanel   
         */
	public OvalPanel getWelcomePane() {
		if (welcomePane == null) {
			welcomePane = new OvalPanel(3, 3, new Dimension(this.getWidth(), 0));
			welcomePane.add(getWelcomeLbl(), BorderLayout.CENTER);
 
		}
		return welcomePane;
	}		
}
désolé si j'ai trop écrit
mais vraiment il y a plus qu'un mois que je galère avec les événements, à chaque fois j'essaye un exemple et ça marche parce que je construit tout dans une seule classe. Mais dans mon cas je fais appel à des classes( des classes imbriqués).
merci d'avance