File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes [swing]highlight text on mouse over Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "[swing]highlight text on mouse over" Watch "[swing]highlight text on mouse over" New topic
Author

[swing]highlight text on mouse over

dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Hello i would like to have that effect on some component, but i dont know which component would be the most suitable for it, a JLabel or JPanel. The effect should be like a html link. What is the most suitable way of doing it ?
Thank you for helping.

I precise that i also want an onclick event on the text written.
[ November 06, 2007: Message edited by: dav mrazek ]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

JButton would be the most appropriate - it already has a "rollover" effect (for icons - you'd have to add a mouse listener to highlight text) and performs actions on mouse click. You can also remove borders on JButtons if you want them to look less "button-like".


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
I thought so but isnt it too much work to achieve this with JButton as i dont want border, i want to set the background color as well as the text color, having the "rollover" effect only on the text and not on the whole button ?
Thank you.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

For a button, the majority of those are just method calls, as opposed to having to implement "button-like" features in a JLabel or JPanel. Here's a quick example of making a JButton into a "Link" - the only part I had to implement was to rollover to change the text color:
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Hello and WoW! Thanks a lot for this help, it helps greatly to get started. I was searching also among the swing component and i found the AbstractButton class. Would it be better to use this one as it has all the method that JButton has but i guess it has no background set or other things like this ?
Do you think that extending this class instead of JButton would be a better choice as i would have just to add the behaviour and painting i want and not remove the one i dont want ? I would be interested in seeing the code source for JButton to understand a bit better how it works really. Is this code open source now ?
Thank you.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by dav mrazek:
I was searching also among the swing component and i found the AbstractButton class. Would it be better to use this one as it has all the method that JButton has but i guess it has no background set or other things like this ?
Do you think that extending this class instead of JButton would be a better choice as i would have just to add the behaviour and painting i want and not remove the one i dont want ?


I don't think AbstractButton would be all that helpful. As an abstract class it has no UI Delegates, so extending it directly doesn't do much.

Why the aversion to using JButton? It includes methods such as setBorderPainted() and setContentAreaFilled() for a reason.

I would be interested in seeing the code source for JButton to understand a bit better how it works really. Is this code open source now ?


Even before it was open-sourced, the source code was easily available.
You could download it from sun or find it on sites like this.

[edit: You mention "painting." I don't think you would have to mess with the paint methods--just change the font and/or color.]
[ November 09, 2007: Message edited by: Brian Cole ]

bitguru blog
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Alright, thank you for your answer, i tried to create a new class that extend AbstractButton to see how this class is behaving by default and it just does nothing helpful for me and it would be too much work to do utilizing this abstract class. So thank you for your help and advice.
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Originally posted by Brian Cole:


[edit: You mention "painting." I don't think you would have to mess with the paint methods--just change the font and/or color.]

[ November 09, 2007: Message edited by: Brian Cole ]


Yes i think that i have to at least overide JComponent paintComponent(Graphics) if i want to change the background and foreground color for the button.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by dav mrazek:
Yes i think that i have to at least overide JComponent paintComponent(Graphics) if i want to change the background and foreground color for the button.


Ok, or you could just call setForeground() and setBackground().
dav mrazek
Ranch Hand

Joined: Aug 11, 2007
Posts: 69
Hello, here is what i did. I have done it by extending JComponent, cause it just seemed easier to me, maybe i m wrong.

[ November 11, 2007: Message edited by: dav mrazek ]
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by dav mrazek:
here is what i did. I have done it by extending JComponent, cause it just seemed easier to me, maybe i m wrong.


Well your approach certainly works, though you need to implement getPreferredSize(). [Also, it might be better to move new Font(...) outside of paintComponent().]

The reason some of us here suggested subclassing JButton is just because we thought it would be easier. For example, I just threw together this:



You'll have to decide for yourself whether you think this approach is better and/or easier for you.



[edit: Oops, though there are some differences, I see that my code is similar to Nathan Pruett's. Sorry for the duplication.]
[ November 11, 2007: Message edited by: Brian Cole ]
Hemanth Balaji
Greenhorn

Joined: Jan 02, 2006
Posts: 8
Nathan Pruett wrote:For a button, the majority of those are just method calls, as opposed to having to implement "button-like" features in a JLabel or JPanel. Here's a quick example of making a JButton into a "Link" - the only part I had to implement was to rollover to change the text color:


Hello Nathan,

This is a great Peice of code which I could also use to make my Jbutton to look like a hyperlink. On Click of the Hyperlink I would need to open up a Dialog. However I need to have some tweaks in here. The Label on the JButton should show a Hand Cursor when Hovered upon my Mouse Enter and Mouse Exit to the Normal Curson. How do I get the Hand Cursor

Regards,
Hemanth

Update: I got it.. Just Added setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); in mouseEntered. But Nathan, Thanks for the Code. Neat Stuff.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I haven't given this a lot of thought, but it strikes me that someone might implement any kind of a rollover effect they wanted, on just about any kind of component, by adding a mouse listener to the component, then making appropriate use of mouseEntered() and mouseExited() methods, as well as either paint() or paintComponent() method.

It seems pretty intuitively obvious idea, so maybe there is something wrong with it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [swing]highlight text on mouse over