Hello
I'd like to create a hover effect for JButtons, but i can't find it for the moment.
It doesn't have to be antything spectacular, it just has to be visible.
Thanks in advance.
Greets, K?!.
Hello
I'd like to create a hover effect for JButtons, but i can't find it for the moment.
It doesn't have to be antything spectacular, it just has to be visible.
Thanks in advance.
Greets, K?!.
If you just need a visible hover, you do not have to write a MouseListener. Swing buttons support rollovers. For image-only buttons, enable rollover and supply an alternate icon for hover. This is supported by most Look & Feels and avoids manual event code. See AbstractButton.setRolloverEnabled.
JButton b = new JButton();
b.setIcon(defaultIcon);
b.setRolloverEnabled(true);
b.setRolloverIcon(hoverIcon); // shown while hovered
b.setPressedIcon(pressedIcon); // optional
b.setBorderPainted(false); // off by default
b.setFocusPainted(false); // cleaner look for image buttons If you prefer a non-icon effect (eg. border or underline), add a ChangeListener per button and toggle UI based on the model’s rollover state rather than tracking mouse enter/exit yourself. This keeps the logic consistent with Swing’s button model. See ChangeListener and How to Use Buttons.
ButtonModel m = b.getModel();
m.addChangeListener(e -> {
boolean over = m.isRollover();
b.setBorderPainted(over); // show border only on hover
// or update text with HTML: b.setText(over ? "<html><u>Play</u></html>" : "Play");
}); For your array of 12 buttons, attach the index as metadata and read it in any shared listener instead of relying on action commands:
b.putClientProperty("cardIndex", i);
// later in a listener (Action or Mouse)
int idx = (int) ((JButton) e.getSource()).getClientProperty("cardIndex"); Reference: JComponent.putClientProperty.
Jump to Post— majestic0110 187Do you mean a tooltip, where text is displayed over a "hovered " button ?
Jump to Post— Ezzaral 2,714You can do whatever you want when the mouse enters or leaves the button
jButton1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseEntered(java.awt.event.MouseEvent evt) { jButton1.setBackground(Color.GREEN); } public void mouseExited(java.awt.event.MouseEvent evt) { jButton1.setBackground(UIManager.getColor("control")); } });or to just underline
jButton1.addMouseListener(new java.awt.event.MouseAdapter() { Font originalFont = null; public …
Jump to Post— freshface001 0hi,
first add mouse listener to the button.
and change the background to the button in the following function
public void mouseEntered(MouseEvent e){
button.setBackground(Color.cyan);
}
public void mouseExited(MouseEvent e){
button.setBackground(Color.gray);
}
you also change font size,font color,etc;try it..
Do you mean a tooltip, where text is displayed over a "hovered " button ?
No, something that changes about the button.
Like a link on a webpage: e.g. it gets underlined when the arrow hovers above it.
Thanks in advance.
K?!
You can do whatever you want when the mouse enters or leaves the button
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jButton1.setBackground(Color.GREEN);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jButton1.setBackground(UIManager.getColor("control"));
}
}); or to just underline
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
Font originalFont = null;
public void mouseEntered(java.awt.event.MouseEvent evt) {
originalFont = jButton1.getFont();
Map attributes = originalFont.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
jButton1.setFont(originalFont.deriveFont(attributes));
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jButton1.setFont(originalFont);
}
}); hi,
first add mouse listener to the button.
and change the background to the button in the following function
public void mouseEntered(MouseEvent e){
button.setBackground(Color.cyan);
}
public void mouseExited(MouseEvent e){
button.setBackground(Color.gray);
}
you also change font size,font color,etc;
try it..
Thanks for the help already.
It should work but there's still one problem.
I'm using an array of buttons (12). Somehow i'd have to be able to access the number of the button in the array. I was told to use the actionCommand for the actionListener. Like that i was able to use "getActionCommand" on the event to get the card's number.
Any options to get the card's number from the MouseEvent?
Thanks in advance.
Greets, K?!
getComponent() will provide the component that initiated the mouse event. If the info you need is just the text on the button then this will get it
public void mouseExited(java.awt.event.MouseEvent evt) {
JButton button = (JButton)evt.getComponent();
System.out.println(button.getText());
} If you need some other info, you can extend JButton to keep a reference to your Card object (or whatever object the button represents) and cast to that object instead.
Well, the problem is, the buttons don't have any text. Only an image of a card.
That's why i was told to use "setActionCommand()" and "getActionCommand()".
I tried to use getActionCommand() on the component (the button that was obtained by "getComponent()" ofcourse).
But it doesn't seem to be possible.
There's one thing i don't understand:
If you need some other info, you can extend JButton to keep a reference to your Card object (or whatever object the button represents) and cast to that object instead.
I don't know how i should do that, and if it's difficult, i'd like a simple solution because we almost have to finish the project.
Is there any way to get the action command? I used that before and the action commands already contain the right numbers.
Thanks for the help.
And thanks in advance for further help.
Greets, K?!.
By the way, freshface001,
I thought your suggestion was the same like Ezzaral's, so i decided to copy his code instead.
Is it really the same or am i wrong?
Thanks for helping.
Greets.
Ok guys, found it :).
In stead of trying to do this:
btnCard[evt.getComponent().getActionCommand()].setBackground(Color.GREEN); I can do this:
evt.getComponent().setBackground(Color.GREEN); Thanks for helping me out :).
Greets K?!
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.