Drazen said:
This is not JButton overriden, but it might give you some ideas.
This is a nice looking hyperlink component derived from JComponent.
The code is mine and it is just released under BSD license.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ActiveLabel extends JComponent {
public final int XMARGIN=2,YMARGIN=2;
public final int ACTIVE=1,IDLE=0,PRESSED=2;
static final Color[] clr = { Color.blue, Color.red, Color.black };
ActionListener lst;
Thanks for contributing your code, Drazen!
A little feedback for you...
Are you aware that class members without an explicit access modifier
(private, protected or public) are made public to other classes in the
same package? So any class in the same package can reach in and directly
read (or modify) your ActionListener list which breaks encapsulation. If
something should be private, I suggest you make it explicitly private.
Also, I suggest that you mark your constants 'static' as well as final,
e.g.:
public static final int XMARGIN=2,YMARGIN=2;
^^^^^^
[snip]
public void paint(Graphics g)
{
FontMetrics fm=getFontMetrics(getFont());
[snip]
Btw, it's usual to override paintComponent rather than paint, unless you
have good reason. By overriding paint directly you are breaking the
rendering of any borders that may have been set on the component...
Also I notice your component has its text set at construction time, and
the text doesn't change thereafter. So it's a little wasteful to do
those calculations on the size of the text etc. each and every time
paint gets called (or getMinimumSize) - you may as well siphon off that
work into a seperate method, and only call it the first time it is
needed, and then cache the results for later reuse.