import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.*;The applet uses similar constructs as the SimpleApplet discussed earlier on:
...
private TextField URLText = null ;
private Button URLButton = null;
...
public void init()
{
add(new Label("URL"));
URLText = new TextField(30);
add(URLText);
URLButton = new Button("Go");
add(URLButton);
...
We can see that additionally a label, which labels the textfield,
is added to the applet panel.
public class URLApplet extends Applet
implements ActionListener
...
URLButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (e.getSource() instanceof Button)
if (actionCommand.equals("Go"))
{
The sequence of tests within the event handler actionPerformed
is also the same.
System.out.println("Go");
try
{
AppletContext context = getAppletContext();
URL url = new URL(URLText.getText());
context.showDocument(url);
}
catch(Exception ex)
{
showStatus("Error "+ex);
}
The first line is only a test output (you can delete it if you want).
getAppletContext is a method of the Applet class which determines the applet context - information about the environment it is running in. AppletContext is an interface which can be used by an applet to determine concrete information about the context (are there other applets: getApplets() ) or to affect the context. The showDocument(..) method is an example of the latter class.
It requests the browser to show the Web page specified by the URL. URLText is a textfield object. Applying the getText() method gives us the string typed in by the user. If this is actually a valid URL, then a URL object url is created.
Here is the full text of the applet:
//
// an Applet showing a Web page
// Claus Pahl
// Feb 2000
//
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class URLApplet extends Applet
implements ActionListener
{
private TextField URLText = null ;
private Button URLButton = null;
public URLApplet()
{
}
public void init()
{
add(new Label("URL"));
URLText = new TextField(30);
add(URLText);
URLButton = new Button("Go");
add(URLButton);
URLButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (e.getSource() instanceof Button)
if (actionCommand.equals("Go"))
{
System.out.println("Go");
try
{
AppletContext context = getAppletContext();
URL url = new URL(URLText.getText());
context.showDocument(url);
}
catch(Exception ex)
{
showStatus("Error "+ex);
}
}
}
}
URL url = new URL(URLText.getText()); URLConnection connection = url.openConnection();
DataInputStream dis = new DataInputStream(connection.getInputStream());This input stream can then be used to write the file to the terminal or to a file, or to do something else with it.
DataOutputStream dos = new DataOutputStream(connection.getOutputStream());An application of this idea might establish an output connection to the URL, close it, and then open an input stream handling data send back from the contacted server.
setLayout(new BorderLayout());This defines a layout with a Center field and 4 fields around it (called North, East, South and West). Now you can add GUI components at specific places, e.g.
add("Center", URLButton);
or
add("North",URLText);
Within the init method, create the button object:
CheckButton = new Button("Check");
add(CheckButton);
Then you need to implement the action handling.
As for the URLbutton, the event type is action and the
event generating object is of type button.
So within the
if (e.getSource() instanceof Button) { ... }
statement, add an event handler for the check button:
if (actionCommand.equals("Check"))
{
...
}
The content of the textfield shall be checked.
The first step is to get the content of the textfield.
We could require that the URL connection uses HTTP as
the protocol. So we can compare the first four letter
of the textfield content with 'http'.
If there is something wrong we will print an error
message.
String s;
s = URLText.getText();
s = s.substring(1,4);
if ( !s.equalIngnoreCase("http") ) {
errorMsg = new label("ERROR: not HTTP");
add(errorMsg);
validate();
}
You need to declare 'errorMsg' as a 'Label'.
'validate()' forces the applet to redraw the panel.
If you are using the graphics package, then use 'repaint()'.
As explained earlier on, ENTER (return) in a textfield causes an action event. This can be dealt with in the same way as action events caused by buttons - only the instanceOf operation should test 'TextField' and not 'Button'.
© 2000 Claus Pahl