We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Trying to create text links

rm21
rm21 asked
on
Medium Priority
309 Views
Last Modified: 2010-03-31
I'm working on a project and have become stuck. I want to display the contents of an arraylist as a series of adjacent, underlined pieces of text that execute some code when you click on them. I initially displayed the contents as a row of JButtons (with border set to null and editable false). They were quite large however, and I wanted the text to be close together. I tried reducing the size of the JButton using setSize, but whenever its size was set beneath a certain value, the text on the button changed into "...", even though there was blatantly more than enough room to fit the text on. Reducing the font size of the text did not help either.

I'm now trying to use a JTextField (with border set to null and editable false) to display the contents. Although it is possible to add an ActionListener to this, I cannot figure out how to catch the events it generates. The only one i'm interested in is when the mouse is clicked on it. I presume it generates an event because it highlights the current contents of the textfield. I realise it is possible to do this by adding a MouseMotionListener and detecting its clicks, but i was wondering if there was a simpler way.

Is there any way to do this or should I try creating hyperlinks with a jeditorpane?

Thanks
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Use a JEditorPane, and HyperlinkListener to listener for click.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
You would use html and <a> tags for your links.

Author

Commented:
How would I create a hyperlink that generates events then?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
In your html use the <a> tag

Author

Commented:
Have got a content pane running with some text but what would i put between the <a></a> tags to get some code to run? And subsequently what would i tell the hyperlinklistener to listen for? Thanks again
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
<a href="url">link text</a>

the url will be passed to the hyperlinklistener in the HyperlinkEvent whenever the link is pressed.


Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thanks guys for the help

Commented:
thnx ;P
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
:(
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.