Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to make JButton border rollover?

843806Nov 20 2008 — edited Nov 21 2008
Hello,

when you move the pointer across the edge of a default JButton, the border changes, between one-pixel wide, and 3-pixel wide shaded. If you want to do this effect yourself by hand, how - is there a method to do this?

Before you ask "why" - this is the context, if you care to know why I need this:



I had written here yesterday that JButton on a Mac looks way too long for the label. If I have a label "?", the button is about 100 pixels! This has something to do with the Mac L&F...

So I figured out, that if I set the border on the button, then the button is tight around the label, on both PC and Mac. Which is good, but ... now it's too tight. So I wanted to create margins - and of course, you can't have both setMargin and setBorder on a JButton (per docs). You have to make a compound border with an empty inner border.
JButton helpButton = new JButton("?");
Border innerBorder =
                BorderFactory.createEmptyBorder( 3, 3, 2, 3 );
Border outerBorder = new LineBorder(Color.gray, 1, false);  
Border compoundBorder =
                BorderFactory.createCompoundBorder( outerBorder, innerBorder );
helpButton.setBorder(compoundBorder); 
OK, now it's looking good, but, the only problem is it lost its rollover effect, when you move your pointer across the border, nothing changes. So I guess I need to do this by hand as well. Maybe there is a method for it, rather than have to implement mouse event handlers and such.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 19 2008
Added on Nov 20 2008
4 comments
312 views