Clickable link in an application/applet

Discussion in 'Java' started by Aaron Fude, Dec 28, 2007.

  1. Aaron Fude

    Aaron Fude Guest

    Hi,

    It seems like it would be a common question, but I have not been able
    to find a good reference.

    I would like to have a clickable URL link in a JLabel. It's OK if the
    whole label is a link.

    Ideally, the solution is implemented identically if the label is used
    in an applet or an application. I would like to be able to specify the
    target. The linked page should be displayed by the user's default
    browser.

    Many thanks in advance!

    Aaron Fude
     
    Aaron Fude, Dec 28, 2007
    #1
    1. Advertising

  2. Aaron Fude

    Mark Space Guest

    Mark Space, Dec 28, 2007
    #2
    1. Advertising

  3. Aaron Fude wrote:
    > Hi,
    >
    > It seems like it would be a common question, but I have not been able
    > to find a good reference.
    >
    > I would like to have a clickable URL link in a JLabel. It's OK if the
    > whole label is a link.
    >
    > Ideally, the solution is implemented identically if the label is used
    > in an applet or an application. I would like to be able to specify the
    > target. The linked page should be displayed by the user's default
    > browser.
    >
    > Many thanks in advance!
    >
    > Aaron Fude


    This will work in an Applet but I'm sure not in an application. I
    remember asking this question years ago but I don't remember the answer
    I got. It may have been better than this one :).

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.lang.reflect.*;
    import java.net.*;
    import javax.swing.*;

    public class test2 extends JApplet {
    public void init() {
    try {
    EventQueue.invokeAndWait(new Runnable() {
    public void run() {
    JLabel l = new JLabel("Knute's Website");
    l.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent me) {
    AppletContext ac =
    test2.this.getAppletContext();
    try {
    ac.showDocument(new URL(
    "http://www.knutejohnson.com"));
    } catch (MalformedURLException murle) {
    murle.printStackTrace();
    }
    }
    });
    add(l);
    }
    });
    } catch (InvocationTargetException ite) {
    ite.printStackTrace();
    } catch (InterruptedException ie) {
    ie.printStackTrace();
    }
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Dec 28, 2007
    #3
  4. Aaron Fude wrote:
    ...
    >It seems like it would be a common question, but I have not been able
    >to find a good reference.


    It is a common question with no simple, straight forward answer.

    >I would like to have a clickable URL link in a JLabel.


    What sort of information are you opening in the link?
    Is it from the your site? Do you have control over it?

    >..It's OK if the whole label is a link.


    This is easy enough with regular label with a MouseListener
    attached, the mouseEntered event can be used to show the link
    as 'active'.

    >Ideally, the solution is implemented identically if the label is used
    >in an applet or an application. I would like to be able to specify the
    >target.


    That makes it tricky. None of the methods of opening a browser
    from an *application* include the option to specify 'target'.

    >..The linked page should be displayed by the user's default
    >browser.


    OK, you ready?

    Applet can use the showDocument method, but browsers never
    had to implement it, there is no report of success or failure, and
    many pop-up killers interfere with it. It does allow to specify a
    target, which can be ignored by the browser if it chooses.

    I would avoid it, if the information in the links is anything more
    than trivial.

    Applications.

    Pre 1.6 applications would have to rely on BrowserLauncher2
    (3rd party APIs), while 1.6+ can use the Desktop.browse()
    functionality. Desktop reports success/failure, AFAIR, BL2
    throws exceptions if it cannot find(/launch) the browser.

    JWS (J2SE 1.2+) applications or applets have access to
    BasicService.showDocument()..
    <http://www.physci.org/jws/#bs>

    Ultimately, it is easiest in a JWS app.

    --
    Andrew Thompson
    http://www.physci.org/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
     
    Andrew Thompson, Dec 29, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oscar
    Replies:
    1
    Views:
    579
    Andrew Thompson
    Nov 24, 2004
  2. Randyl
    Replies:
    1
    Views:
    434
    Andrew Thompson
    Oct 13, 2007
  3. mi
    Replies:
    4
    Views:
    13,705
    dorayme
    May 21, 2008
  4. Jiong Feng
    Replies:
    1
    Views:
    188
    Lasse Reichstein Nielsen
    May 15, 2004
  5. Clickable mail link?

    , Jan 26, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    117
    Charlton Wilbur
    Jan 26, 2007
Loading...

Share This Page