**URGENT HELP REQUIRED** Embeding hyperlinks in Applets

Hi guys.... Say my applet displays the text... " To go to this page click here" . ..... I want.. "click here" to be a be a hot link.. like in HTML .. one clicks on the text.. and the desired link is followd. How do I go about doing that in Applets?
[255 byte] By [arijit_dattaa] at [2007-10-3 4:05:25]
# 1

It depends.

I did that once on a scrolling ticker app by pre-generating an image of the text, and calculating what pixel ranges in the image corresponded to the link. Then when I got a mouse click I figured out the current relative position of the image and the position of the mouse click over that image.

You could just use a button.

As I understand it some of the more advanced Swing stuff lets you have hypertext inside of JTextAreas, but I don't know about the details. Ask on a Swing forum.

In AWT text widgets have a getCaretPosition method that you can use to see where the user clicked, perhaps.

In any event, in applets, you can do getAppletContext().showDocument(URL) to go to other pages.

paulcwa at 2007-7-14 22:04:45 > top of Java-index,Desktop,Core GUI APIs...
# 2

Use a simple JLabel and showDocument:

import java.applet.AppletContext;

import java.net.URL;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class HyperlinkApplet extends JApplet {

JLabel jLabel = null;

URL url = null;

AppletContext ac = null;

public HyperlinkApplet() {

try {

jLabel = new JLabel("<html><u>Hyperlink to Java</u></html>");

url = new URL("http://java.sun.com/");

jLabel.setForeground(Color.blue);

jLabel.setToolTipText(url.toString());

jLabel.setHorizontalAlignment(SwingConstants.CENTER);

jLabel.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseEntered(MouseEvent e) {

jLabel_mouseEntered(e);

}

public void mouseExited(MouseEvent e) {

jLabel_mouseExited(e);

}

public void mouseClicked(MouseEvent e) {

jLabel_mouseClicked(e);

}

});

this.getContentPane().add(jLabel, BorderLayout.CENTER);

}

catch(Exception e) {

e.printStackTrace();

}

}

public void init() {

try {

this.setSize(new Dimension(100,800));

ac = this.getAppletContext();

}

catch(Exception e) {

e.printStackTrace();

}

}

void jLabel_mouseEntered(MouseEvent e) {

jLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));

}

void jLabel_mouseExited(MouseEvent e) {

jLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

}

void jLabel_mouseClicked(MouseEvent e) {

ac.showDocument(url, "_blank");

}

}

attilaracza at 2007-7-14 22:04:45 > top of Java-index,Desktop,Core GUI APIs...
# 3
You don't need to set the cursor on entry and exit - that's done automatically, just set it once on the label.
itchyscratchya at 2007-7-14 22:04:45 > top of Java-index,Desktop,Core GUI APIs...