User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,590 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,663 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 824 | Replies: 9 | Solved
Reply
Join Date: Apr 2008
Posts: 20
Reputation: K?! is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
K?! K?! is offline Offline
Newbie Poster

Creating Hover Effect for JButton

  #1  
May 13th, 2008
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?!.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Bristol, UK
Posts: 1,124
Reputation: majestic0110 is on a distinguished road 
Rep Power: 3
Solved Threads: 47
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Veteran Poster

Re: Creating Hover Effect for JButton

  #2  
May 13th, 2008
Do you mean a tooltip, where text is displayed over a "hovered " button ?
If you have a quality, be proud of it and let it define you. Add it to the world!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
Reply With Quote  
Join Date: Apr 2008
Posts: 20
Reputation: K?! is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
K?! K?! is offline Offline
Newbie Poster

Re: Creating Hover Effect for JButton

  #3  
May 13th, 2008
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?!
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,843
Reputation: Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all 
Rep Power: 12
Solved Threads: 283
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: Creating Hover Effect for JButton

  #4  
May 13th, 2008
You can do whatever you want when the mouse enters or leaves the button
  1. jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
  2. public void mouseEntered(java.awt.event.MouseEvent evt) {
  3. jButton1.setBackground(Color.GREEN);
  4. }
  5. public void mouseExited(java.awt.event.MouseEvent evt) {
  6. jButton1.setBackground(UIManager.getColor("control"));
  7. }
  8. });
or to just underline
  1. jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
  2. Font originalFont = null;
  3.  
  4. public void mouseEntered(java.awt.event.MouseEvent evt) {
  5. originalFont = jButton1.getFont();
  6. Map attributes = originalFont.getAttributes();
  7. attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
  8. jButton1.setFont(originalFont.deriveFont(attributes));
  9. }
  10.  
  11. public void mouseExited(java.awt.event.MouseEvent evt) {
  12. jButton1.setFont(originalFont);
  13. }
  14. });
Reply With Quote  
Join Date: Apr 2008
Posts: 16
Reputation: freshface001 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
freshface001 freshface001 is offline Offline
Newbie Poster

Re: Creating Hover Effect for JButton

  #5  
May 14th, 2008
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 and regards,
Suresh Kumar.R
Reply With Quote  
Join Date: Apr 2008
Posts: 20
Reputation: K?! is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
K?! K?! is offline Offline
Newbie Poster

Re: Creating Hover Effect for JButton

  #6  
May 14th, 2008
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?!
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,843
Reputation: Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all 
Rep Power: 12
Solved Threads: 283
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: Creating Hover Effect for JButton

  #7  
May 14th, 2008
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.
Reply With Quote  
Join Date: Apr 2008
Posts: 20
Reputation: K?! is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
K?! K?! is offline Offline
Newbie Poster

Re: Creating Hover Effect for JButton

  #8  
May 15th, 2008
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?!.
Reply With Quote  
Join Date: Apr 2008
Posts: 20
Reputation: K?! is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
K?! K?! is offline Offline
Newbie Poster

Re: Creating Hover Effect for JButton

  #9  
May 15th, 2008
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.
Reply With Quote  
Join Date: Apr 2008
Posts: 20
Reputation: K?! is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
K?! K?! is offline Offline
Newbie Poster

Re: Creating Hover Effect for JButton

  #10  
May 15th, 2008
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?!
Last edited by K?! : May 15th, 2008 at 8:41 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Java Forum

All times are GMT -4. The time now is 10:43 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC