Make a button a link?

I have a buttons in a java applet that, when clicked, display text. I want to make the buttons link to a web page instead. How would I go about doing that?

[162 byte] By [kretchfoopa] at [2007-11-27 11:35:21]
# 1

public void init() {

t = new TextField(20);

t.setText("http://www.google.com");

add(t);

b = new Button("Go to this URL");

add(b);

b.addActionListener(this);

}

...

public void actionPerformed(ActionEvent ae) {

if (ae.getSource() == b) {

try {

getAppletContext().showDocument(new URL(t.getText()));

}

catch (Exception e) {

e.printStackTrace();

}

}

}

RealHowToa at 2007-7-29 17:02:20 > top of Java-index,Desktop,Core GUI APIs...
# 2

Is the web page supposed to pop up in a new window? So far when I click the button, the URL only appears in the textfield. I think I'm missing something here since I added the parts of the code that you provided that I didn't have. Thanks.

kretchfoopa at 2007-7-29 17:02:20 > top of Java-index,Desktop,Core GUI APIs...
# 3

> So far when I click the button, the URL only appears

> in the textfield.

Then you made a mistake.

Here the complete example

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import java.net.*;

public class GotoURLButton extends Applet implements

ActionListener {

Button b;

TextField t;

public void init() {

t = new TextField(20);

t.setText("http://www.google.com");

add(t);

b = new Button("Go to this URL");

add(b);

b.addActionListener(this);

}

public void actionPerformed(ActionEvent ae) {

if (ae.getSource() == b) {

try {

getAppletContext().showDocument(new URL(t.getText()));

}

catch (Exception e) {

e.printStackTrace();

}

}

}

}

As is, the web page will replace the current one. To open a new window, change

getAppletContext().showDocument(new URL(t.getText()));

to

getAppletContext().showDocument(new URL(t.getText()),"_blank");

RealHowToa at 2007-7-29 17:02:20 > top of Java-index,Desktop,Core GUI APIs...
# 4

I only changed this part of the code......

getAppletContext().showDocument(new URL (" http://www.google.com"));

other than that, everything else is the same. I've heard that it is not possible to display a web page inside a text field. Is this true?

kretchfoopa at 2007-7-29 17:02:20 > top of Java-index,Desktop,Core GUI APIs...