1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.gui;
21
22 import java.awt.Graphics;
23 import java.awt.Insets;
24 import java.awt.event.MouseEvent;
25 import javax.swing.Action;
26 import javax.swing.JButton;
27
28 /***
29 * This class provides a toolbar button with an appropriately sized icon.
30 * The border of the button is only visible when the mouse hovers over the
31 * button.
32 */
33 public class ToolBarButton extends JButton {
34
35 private boolean showBorder = false;
36 private boolean showMenuHint;
37
38 public ToolBarButton(Action action)
39 {
40 super(action);
41
42 setContentAreaFilled(false);
43 setText(null);
44 setMargin(new Insets(1, 1, 1, 1));
45
46 putClientProperty("hideActionText", Boolean.TRUE);
47 }
48
49 /***
50 * Returns true, if the mouse is currently over the button.
51 */
52 public boolean isMouseOver()
53 {
54 return showBorder;
55 }
56
57 @Override
58 protected void paintBorder(Graphics g)
59 {
60 if (showBorder) {
61 super.paintBorder(g);
62 }
63 }
64
65 @Override
66 protected void processMouseEvent(MouseEvent e)
67 {
68 super.processMouseEvent(e);
69
70 if (e.getID() == MouseEvent.MOUSE_ENTERED) {
71 showBorder = true;
72 setContentAreaFilled(true);
73 repaint();
74 }
75 else if (e.getID() == MouseEvent.MOUSE_EXITED) {
76 showBorder = false;
77 setContentAreaFilled(false);
78 repaint();
79 }
80 }
81
82 }