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?
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?
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();
}
}
}
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.
> 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");
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?