KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > gui > HighlightJButton


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package com.izforge.izpack.gui;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.event.MouseAdapter JavaDoc;
24 import java.awt.event.MouseEvent JavaDoc;
25
26 import javax.swing.Action JavaDoc;
27 import javax.swing.Icon JavaDoc;
28 import javax.swing.JButton JavaDoc;
29
30 /**
31  * A button that highlights when the button passes over.
32  *
33  * @author Julien Ponge
34  */

35 public class HighlightJButton extends JButton JavaDoc
36 {
37
38     private static final long serialVersionUID = 3833184718324969525L;
39
40     /**
41      * The constructor (use ButtonFactory to create button).
42      *
43      * @param icon The icon to display.
44      * @param color The highlight color.
45      */

46     HighlightJButton(Icon JavaDoc icon, Color JavaDoc color)
47     {
48         super(icon);
49         initButton(color);
50     }
51
52     /**
53      * The constructor (use ButtonFactory to create button).
54      *
55      * @param text The text to display.
56      * @param color The highlight color.
57      */

58     HighlightJButton(String JavaDoc text, Color JavaDoc color)
59     {
60         super(text);
61         initButton(color);
62     }
63
64     /**
65      * The constructor (use ButtonFactory to create button).
66      *
67      * @param text The text to display.
68      * @param icon The icon to display.
69      * @param color The highlight color.
70      */

71     HighlightJButton(String JavaDoc text, Icon JavaDoc icon, Color JavaDoc color)
72     {
73         super(text, icon);
74         initButton(color);
75     }
76
77     /**
78      * The constructor (use ButtonFactory to create button).
79      *
80      * @param a The action.
81      * @param color The highlight color.
82      */

83     HighlightJButton(Action JavaDoc a, Color JavaDoc color)
84     {
85         super(a);
86         initButton(color);
87     }
88
89     /**
90      * Does the extra initialisations.
91      *
92      * @param highlightColor The highlight color.
93      */

94     protected void initButton(Color JavaDoc highlightColor)
95     {
96         this.highlightColor = highlightColor;
97         defaultColor = getBackground();
98
99         addMouseListener(new MouseHandler());
100     }
101
102     /**
103      * Overriden to ensure that the button won't stay highlighted if it had the mouse over it.
104      *
105      * @param b Button state.
106      */

107     public void setEnabled(boolean b)
108     {
109         reset();
110         super.setEnabled(b);
111     }
112
113     /** Forces the button to unhighlight. */
114     protected void reset()
115     {
116         setBackground(defaultColor);
117     }
118
119     /** The highlighted color. */
120     protected Color JavaDoc highlightColor;
121
122     /** The default color. */
123     protected Color JavaDoc defaultColor;
124
125     /**
126      * The mouse handler which makes the highlighting.
127      *
128      * @author Julien Ponge
129      */

130     private class MouseHandler extends MouseAdapter JavaDoc
131     {
132
133         /**
134          * When the mouse passes over the button.
135          *
136          * @param e The event.
137          */

138         public void mouseEntered(MouseEvent JavaDoc e)
139         {
140             if (isEnabled()) setBackground(highlightColor);
141         }
142
143         /**
144          * When the mouse passes out of the button.
145          *
146          * @param e The event.
147          */

148         public void mouseExited(MouseEvent JavaDoc e)
149         {
150             if (isEnabled()) setBackground(defaultColor);
151         }
152     }
153 }
154
Popular Tags