Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Look like a hyperlink, behave like a button...

29 views
Skip to first unread message

Dag Sunde

unread,
Feb 10, 2005, 5:52:56 AM2/10/05
to
Can I us a JLabel or something else to make something look
like a html hyperlink (underline, hand cursor...),
and trigger an actionEvent like a JButton?

(Hmpfff... Managers with design ideas...)

TIA...

--
Dag.


igyy

unread,
Feb 10, 2005, 6:03:57 AM2/10/05
to

I am not sure, just check in Javadoc if JLabel is implementing
ActionListener (JButton is)...if does then you can make something like
that

Thomas Fritsch

unread,
Feb 10, 2005, 7:13:51 AM2/10/05
to

http://www.google.com/search?q=JLinkLabel

--
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Andrew Thompson

unread,
Feb 10, 2005, 7:15:02 AM2/10/05
to
On Thu, 10 Feb 2005 10:52:56 GMT, Dag Sunde wrote:

> Can I us a JLabel or something else to make something look
> like a html hyperlink (underline, hand cursor...),
> and trigger an actionEvent like a JButton?

You're in luck. Just having come from a c.l.j.? convo.
about links, I writ this..

<sscce>
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class LinkLabel extends Label implements MouseListener {

URL url;

final Color normal = Color.blue;
final Color hover = Color.red;
final Color active = Color.magenta;
Color mouseOutDefault;

public LinkLabel(String text, URL url) {
super(text);
this.url = url;
this.addMouseListener(this);
setForeground( normal );
mouseOutDefault = normal;
}

public void paint(Graphics g) {
super.paint(g);
g.drawLine( 2,
getHeight()-5,
getPreferredSize().width-12,
getHeight()-5 );
}

public void mouseReleased(MouseEvent me) {}

public void mousePressed(MouseEvent me) {
mouseOutDefault = active;
}

public void mouseExited(MouseEvent me) {
setForeground( mouseOutDefault );
System.out.println( "Clear 'status bar'" );
}

public void mouseEntered(MouseEvent me) {
setForeground( hover );
System.out.println( "Display '" + url + "' in 'status bar'" );
}

public void mouseClicked(MouseEvent me) {
setForeground( active );
mouseOutDefault = active;
System.out.println( "Open '" + url + "' now!" );
}

public static void main(String[] args)
throws MalformedURLException {

Frame f = new Frame("Link Label Demo");
f.setLayout(new GridLayout(0,1));
f.add( new LinkLabel("PhySci Web & IT help",
new URL("http://www.physci.org/codes/")) );
f.add( new LinkLabel("Sun Microsystems - Developer Network",
new URL("http://java.sun.com")) );
f.add( new LinkLabel("Some Other Site",
new URL("http://www.someothersite.com")) );

f.pack();
f.setVisible(true);
}
}
</sscce>

> (Hmpfff... Managers with design ideas...)

Ya' get that. ;-)

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane

Dag Sunde

unread,
Feb 10, 2005, 8:34:47 AM2/10/05
to
"Andrew Thompson" <SeeMy...@www.invalid> wrote in message
news:15g5wdyosaqgf.1pcboa0x63032$.dlg@40tude.net...

> On Thu, 10 Feb 2005 10:52:56 GMT, Dag Sunde wrote:
>
> > Can I us a JLabel or something else to make something look
> > like a html hyperlink (underline, hand cursor...),
> > and trigger an actionEvent like a JButton?
>
> You're in luck. Just having come from a c.l.j.? convo.
> about links, I writ this..
>
> <sscce>
> import java.awt.*;
> import java.awt.event.*;
> import java.net.*;
>
> public class LinkLabel extends Label implements MouseListener {


You're da' man, Andrew!

Thank you.

--
Dag.


Andrew Thompson

unread,
Feb 11, 2005, 4:02:29 AM2/11/05
to
On Thu, 10 Feb 2005 13:34:47 GMT, Dag Sunde wrote:

> You're da' man, Andrew!
>
> Thank you.

You're welcome.

[ But I suspect JLinkLabel as linked by Thomas might be the best way to
go. The author probably wrote some design for it & spent more than 20
minutes coding, and 3 testing, it. I was almost going to do a search
myself but decided I actually wanted to see if my (simple) approach
would work. ;-) ]

0 new messages