rollover effect on a button?

S

Sandro

Hi,

I want a border appears when I roll over with the mouse over a button.
And when I quit the button area the border should be disappeared. At
first I resolved the problem by setting a LineBorder in the
mouseEntered method and setting an EmptyBorder in the mouseExited
method. However the border is drawed around the hole button. But I
want only the icon at the button surrounded by a border. Therefore I
tried to resolve it in another way which is show in the following
class myButton:

Code:
public class myButton extends JButton {

private boolean isOver = false;
int iconHeight, iconWidth;


public myButton(String s, ImageIcon icon) {
super(s, icon);
iconWidth = icon.getIconWidth();
iconHeight = icon.getIconHeight();

setBorder(BorderFactory.createLineBorder(Color.blue));
setFocusPainted(false);
setRolloverEnabled(true);

addMouseListener(new MouseAdapter() {

public void mouseEntered(MouseEvent e) {
isOver = true;
repaint();
}

public void mouseExited(MouseEvent e) {
isOver = false;
repaint();
}
});

}

public void paint(Graphics g) {
super.paint(g);

if(isOver) {

g.setColor(Color.blue);
g.drawRect(	getX() + getInsets().left,
getY() + getInsets().top,
iconWidth,
iconHeight);

}

}
}

Much to my surprise I found that only the first used instance of the
class myButton shows the implemented behaviour. All afterwards
instanced objects of myButton seem to be without the new extended
paint method. But why? I would be very grateful if you could test the
myButton class and post your experience.

Thx
 
B

Bjorn Abelli

this to c.l.j.h. as well...

Please don't multi-post.

You can read my answer to your post in comp.lang.java.help.


// Bjorn A
 
A

Andrew Thompson

this to c.l.j.h. as well...

Please don't multi-post.

You can read my answer to your post in comp.lang.java.help.

Or my response in c.l.j.gui referring the OP back to it, or..??

I hope, Sandro, that you now understand why people dislike
multi-posting so much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top