-1

This is for an application so I don't want a hyperlink. I first tried using a Jbutton without all of border/background stuff and then hooking up an actionListener to it but I couldn't get it to the point where I thought it looked nice. I also tried using a JLabel and hooking up a mouse listener to that but I also couldn't get it to look right.

Basically I would like a way using swing to make a button exactly like a url link in an application. What is the standard way of doing this?

Grammin
  • 11,808
  • 22
  • 80
  • 138
  • If it's a desktop application, why do you want to use the appearance of hyperlinks anyway? Wouldn't buttons (possibly with an appropriate icon) convey the message even better? – Darien Jun 01 '11 at 21:39
  • -1, we don't know what your think looks nice. Also, what does an application have to do with not using a hyperlink? All a hyperlink does is generate an event when the text is clicked. It doesn't mean you acutally have to read a URL and load a webpage. – camickr Jun 01 '11 at 21:50
  • @camickr I wanted to avoid getting answers that suggest using a hyperlink. Also maybe "Looks nice" is not very descriptive but we can agree that a button with size 4 font is to small to read and therefore doesn't "look nice" and that all the buttons on this page generally "look nice". So when I say "looks nice" I mean "it could pass a professionally developed button" – Grammin Jun 03 '11 at 14:56

4 Answers4

4

but I couldn't get it to the point where I thought it looked nice

You might want to go into greater detail on just what "looked nice" means. I can see you solving this by either a JButton or a JLabel, but the key is perhaps not to look for another solution but to play with the settings of the button or the label til they look nice. If you can't find a nice solution, then post your code (an SSCCE would work best of all) and perhaps we can help you.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

that isn't answer to your question but are you tried to add ButtonModel to your JButton example here

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

It is a rather heavy hammer to use, but SwingX has a JXHyperLink control that is probably exactly what you want. The source is at http://java.net/projects/swingx/sources/svn/content/trunk/swingx-core/src/main/java/org/jdesktop/swingx/JXHyperlink.java?rev=4027 and you can see an article about it at http://www.javalobby.org/java/forums/t18617.html.

It is old, but SwingX continues to do good things.

Femi
  • 64,273
  • 8
  • 118
  • 148
1

It's you're trying to make a desktop application which looks like HTML inside a browser, you might try using some of the richer Swing text components in a read-only mode. You could use a mouse-listener to map X/Y clicks to a particular character of text, and then cause an action to occur on that basis.

Darien
  • 3,482
  • 19
  • 35
  • For any JTextComponent (e.g., JTextArea, JTextPane, JEditorPane, JTextField) a method you'll likely need is [viewToModel](https://docs.oracle.com/javase/8/docs/api/javax/swing/text/JTextComponent.html#viewToModel-java.awt.Point-) to convert from the mouse event's x,y coordinates to a text position (the index of the character). – Joshua Goldberg Nov 26 '18 at 15:38
  • replaced in Java 9 with viewToModel2D – Joshua Goldberg Nov 26 '18 at 16:32