j'ai dessiné quelques rectangles dans ma JPanel.
Comment est-ce possible de gérer 'mouseMoved' sur chaque des ces rectangles afin que le pointeur de la souris prenne la forme de petite main ?
j'ai essayé comme ça:
SimpleTab:
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
 
public class SimpleTab extends Rectangle {
 
	protected int xPos;
	protected int yPos;
	protected int width;
	protected int height;
	protected String title;
 
	public SimpleTab(int xPos, int yPos, int width, int height, String title) {
		super(xPos, yPos, width, height);
 
		this.xPos = xPos;
		this.yPos = yPos;
		this.width = width;
		this.height = height;
		this.title = title;
 
	}
 
	public String getTitle() {
		return title;
	}
 
	public void setTitle(String title) {
		this.title = title;
	}
 
	public int getxPos() {
		return xPos;
	}
 
	public void setxPos(int xPos) {
		this.xPos = xPos;
	}
 
	public int getyPos() {
		return yPos;
	}
 
	public void setyPos(int yPos) {
		this.yPos = yPos;
	}
}
Panel:
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
 
public class Onglets extends JPanel implements MouseListener,
		MouseMotionListener {
 
	protected int x = 10;
	protected int y = 10;
	protected int h = 30;
	protected int w = 200;
	protected int offset = 20;
	protected List<SimpleTab> rectList = new ArrayList<SimpleTab>();
	protected String[] titles;
 
	protected ActionListener actionListener = null;	
 
	public Onglets(String[] titles) {
		addMouseListener(this);
		addMouseMotionListener(this);
		this.titles = titles;
		for (String title : titles) {
			if (title.length() > w) {
				w = title.length();
			}
		}
		for (String title : titles) {
			rectList.add(new SimpleTab(x, y, w, h, title));
			x += w + offset;
		}		
	}
 
	public void addActionListener(ActionListener l) {
		actionListener = AWTEventMulticaster.add(actionListener, l);
 
	}
 
	public void removeActionListener(ActionListener l) {
		actionListener = AWTEventMulticaster.remove(actionListener, l);
	}
 
	@Override
	public void paintComponent(Graphics g) {
 
		Graphics2D g2d = (Graphics2D) g;
 
		g2d.setColor(Color.WHITE);
		g2d.fillRect(0, 0, getWidth(), getHeight());
		for (int i = 0; i < rectList.size(); i++) {
			SimpleTab rectangle = rectList.get(i);
			g2d.setColor(new Color(96, 96, 96));
			if (i == 0) {
				g2d.drawLine(0, rectangle.height + 5, getWidth(),
						rectangle.height + 5);
			}
			g2d.setColor(new Color(240, 240, 240));
			g2d.fillRoundRect(rectangle.x, rectangle.y, rectangle.width,
					rectangle.height + 5, 5, 5);
 
			g2d.setColor(new Color(96, 96, 96));
			g2d.drawRoundRect(rectangle.x, rectangle.y, rectangle.width,
					rectangle.height + 5, 5, 5);
			g2d.drawString(rectangle.getTitle(), rectangle.x + 20,
					rectangle.y + 20);
 
			g2d.setColor(new Color(240, 240, 240));
			g2d.fillRect(0, rectangle.height + 6, getWidth(),
					rectangle.height + 6);
 
			g2d.setColor(Color.WHITE);
			if (i == 0) {
				g2d.drawLine(0, rectangle.height + 6, rectangle.x + 1,
						rectangle.height + 6);
			}
			g2d.drawLine(rectangle.x + rectangle.width - 1,
					rectangle.height + 6, getWidth(), rectangle.height + 6);
			g2d.drawLine(rectangle.x + 1, rectangle.y + 2, rectangle.x + 1,
					rectangle.height + 6);
			g2d.drawLine(rectangle.x + rectangle.width - 1, rectangle.y + 2,
					rectangle.x + rectangle.width - 1, rectangle.height + 6);
			g2d.drawLine(rectangle.x + 2, rectangle.y + 1, rectangle.x
					+ rectangle.width - 2, rectangle.y + 1);
			g2d.drawLine(rectangle.x + rectangle.width - 1, rectangle.y + 2,
					rectangle.x + rectangle.width - 1, 36);
 
		}
 
	}
 
	public void mouseMoved(MouseEvent e) {
		Point point = e.getPoint();
		for (SimpleTab rectangle : rectList) {
 
			if (rectangle.contains(point)) {
				setCursor(new Cursor(Cursor.HAND_CURSOR));
			} else {
				setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			}
		}
 
	}
 
	@Override
	public Dimension getPreferredSize() {
		return new Dimension(1024, 70);
	}
 
	@Override
	public void mousePressed(MouseEvent e) {
 
		for (SimpleTab rectangle : rectList) {
			if (!rectangle.contains(e.getPoint())) {
				return;
			}
		}
 
		if (actionListener != null) {
			ActionEvent actionEvent = new ActionEvent(this, Event.ACTION_EVENT,
					"");
			actionListener.actionPerformed(actionEvent);
		}
 
	}
 
	public void mouseDragged(MouseEvent e) {
	}
 
	public void mouseClicked(MouseEvent e) {
	}
 
	public void mouseReleased(MouseEvent e) {
	}
 
	public void mouseEntered(MouseEvent e) {
	}
 
	public void mouseExited(MouseEvent e) {
	}
 
	public List<SimpleTab> getRectList() {
		return rectList;
	}
 
	public void setRectList(List<SimpleTab> rectList) {
		this.rectList = rectList;
	}
}
Main 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
 
public class Main {
 
	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}
 
	private static void createAndShowGUI() {		
		Onglets onglets = new Onglets(new String[] {"Tab number 1", "Tab 2"/*, "A Very long tab pane title"*/});
        onglets.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Tabbed pane clicked !!");
                return;
            }
        });        
 
        JFrame frame = new JFrame("Custom pane with tabs");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(onglets);
        frame.pack();
        frame.setVisible(true);
 
 
		frame.pack();
		frame.setVisible(true);
	}
 
}
Mais la méthode ne détecte que le dernier Rectangle. En plus, le cllique sur la Tab dessiné ne fonctionne pas... Quelqu'un a 1 idée ? Merci.