I'm trying to build my custom button for a little game, and I use a JLabel
with an icon like this:
JLabel playButton = new JLabel("Play!", Images.getWoodImage(), JLabzl.CENTER);
playButton.setFont(Fonts.getBlueberryFont());
playButton.setHorizontalTextPosition(SwingConstants.CENTER);
playButton.setVerticalTextPosition(SwingConstants.CENTER);
playButton.setBounds(230, 400, 300, 100);
The problem is that I want my buttons to move a little when my cursor enters them; so I don't use default Layouts (I put this.setLayout(null)
). So for my button's MouseListener I use this block of code:
@Override
public void mouseEntered(MouseEvent mouseEvent) {
playButton.setBounds(240, 410, 300, 100); // x + 10 & y + 10
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
playButton.setBounds(230, 400, 300, 100); // reverse the move
}
But it doesn't work the way I want, the button does move but I'm getting this white rectangle behind the icon:
ALSO the same happens when I don't move the button and just change the foregroundColor.
Is there a way I can prevent this without using default layouts?