How do i change the area of focus on a JButton so that when i hover my cursor over the button my costume rollover Icon isn't activated slightly before my cursor is over the actual button itself.
Asked
Active
Viewed 850 times
1 Answers
5
The contains(x, y)
method of the button determines when the mouse enters the button. If your custom button is not rectangular then you would need to override this method.
Here is an old example that demonstrates this concept:
// Old example code found on the internet somewhere
// to paint a Round button with hit detection
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class RoundButton extends JButton
{
public RoundButton(String label)
{
super(label);
// These statements enlarge the button so that it
// becomes a circle rather than an oval.
Dimension size = getPreferredSize();
size.width = size.height = Math.max(size.width, size.height);
setPreferredSize(size);
setSize(size);
// This call causes the JButton not to paint the background.
// This allows us to paint a round background.
setContentAreaFilled(false);
}
// Paint the round background and label.
protected void paintComponent(Graphics g)
{
if (getModel().isArmed()) {
// You might want to make the highlight color
// a property of the RoundButton class.
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0, 0, getSize().width-1, getSize().height-1);
// This call will paint the label and the focus rectangle.
super.paintComponent(g);
}
// Paint the border of the button using a simple stroke.
protected void paintBorder(Graphics g)
{
g.setColor(getForeground());
g.drawOval(0, 0, getSize().width-1, getSize().height-1);
}
// Hit detection.
Shape shape;
public boolean contains(int x, int y)
{
// If the button has changed size, make a new shape object.
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
}
return shape.contains(x, y);
}
private static void createAndShowUI()
{
// Create buttons that overlap one another
JButton button = new RoundButton("Jackpot");
button.setBackground(Color.green);
button.setLocation(0, 0);
JButton button2 = new RoundButton("Jackpot2");
button2.setBackground(Color.red);
button2.setLocation(40, 40);
// Create a frame in which to show the button.
JFrame frame = new JFrame("Round Button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.yellow);
frame.setLayout(null);
frame.add(button);
frame.add(button2);
//frame.setLayout(new FlowLayout());
frame.setLocationByPlatform( true );
frame.setSize(250, 200);
frame.setVisible(true);
MouseListener mouseListener = new MouseAdapter()
{
public void mouseEntered( MouseEvent e ) {}
public void mouseExited( MouseEvent e ) {}
public void mouseClicked( MouseEvent e )
{
System.out.println( "clicked " );
}
public void mousePressed( MouseEvent e )
{
System.out.println( "pressed " );
}
public void mouseReleased( MouseEvent e )
{
System.out.println( "released " );
}
};
button.addMouseListener( mouseListener );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}

camickr
- 321,443
- 19
- 166
- 288
-
1First I was surprised to see you post code to SO, then.. `button.setBounds(0, 0, 100, 100);`.. Are you *really* camickr, or just someone who has managed to hack that account?!? – Andrew Thompson Jun 04 '14 at 03:21
-
1As I said, it is old code. Maybe not the best example to use a null layout, but it does demonstrate how hit detection on Swing components works even when components are stacked on top of one another by overriding the contains(...) method. – camickr Jun 04 '14 at 03:31
-
OK.. Gotta' say, I'm still surprised that you would ever publicly have that code associated with your name, it kind of dents my 'every drop of camickr is pure gold' theory. Not sure whether to have my faith in the universe shaken, or simply enjoy the demonstration that even gurus sometimes take short cuts.. ;) – Andrew Thompson Jun 04 '14 at 03:35
-
3`'every drop of camickr is pure gold' theory` - even gold is never 100% pure ;) – camickr Jun 04 '14 at 03:50