| | |
Creating Hover Effect for JButton
Thread Solved |
You can do whatever you want when the mouse enters or leaves the button or to just underline
java Syntax (Toggle Plain Text)
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")); } });
java Syntax (Toggle Plain Text)
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); } });
•
•
Join Date: Apr 2008
Posts: 18
Reputation:
Solved Threads: 2
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..
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 and regards,
Suresh Kumar.R
Suresh Kumar.R
•
•
Join Date: Apr 2008
Posts: 79
Reputation:
Solved Threads: 0
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?!
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
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.
Java Syntax (Toggle Plain Text)
public void mouseExited(java.awt.event.MouseEvent evt) { JButton button = (JButton)evt.getComponent(); System.out.println(button.getText()); }
•
•
Join Date: Apr 2008
Posts: 79
Reputation:
Solved Threads: 0
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:
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?!.
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.
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?!.
•
•
Join Date: Apr 2008
Posts: 79
Reputation:
Solved Threads: 0
Ok guys, found it
.
In stead of trying to do this:
I can do this:
Thanks for helping me out
.
Greets K?!
.In stead of trying to do this:
btnCard[evt.getComponent().getActionCommand()].setBackground(Color.GREEN); Java Syntax (Toggle Plain Text)
evt.getComponent().setBackground(Color.GREEN);
Thanks for helping me out
.Greets K?!
Last edited by K?!; May 15th, 2008 at 9:41 am.
![]() |
Other Threads in the Java Forum
- Previous Thread: can you see whtas wrong with this one!!
- Next Thread: Word and Character counting (small problem)
Views: 3454 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener add android applet application arguments array arrays binary bluetooth c# c++ chat class classes client code compiler component convert converter coordinates data database db desktop eclipse error event exception fast file fractal givemetehcodez graphics gui helpwithhomework html ide image inheritance input integer j2me java javafx jframe jmf jni jpanel jtable jtextarea jtextfield linked linked-list list loop main map method mobile netbeans newbie node number object objects oracle page pattern print problem program programming read regex remove return robot scanner server set size sms software sort sql string swing text text-file threads timer transfer translate tree url user web windows






