Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Dag Sunde - 10 Feb 2005 10:52 GMT
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...

Signature

Dag.

igyy - 10 Feb 2005 11:03 GMT
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 - 10 Feb 2005 12:13 GMT
> Can I us a JLabel or something else to make something look
> like a html hyperlink (underline, hand cursor...),
[quoted text clipped - 3 lines]
>
> TIA...

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

Signature

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

Andrew Thompson - 10 Feb 2005 12:15 GMT
> 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.   ;-)

Signature

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 - 10 Feb 2005 13:34 GMT
> > Can I us a JLabel or something else to make something look
> > like a html hyperlink (underline, hand cursor...),
[quoted text clipped - 9 lines]
>
> public class LinkLabel extends Label implements MouseListener {

You're da' man, Andrew!

Thank you.

Signature

Dag.

Andrew Thompson - 11 Feb 2005 09:02 GMT
> 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.  ;-) ]

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.