In the code listing below the getFile() function runs when called by init() but not when called by actionPerformed(). I'm strugling trying to figure out how to get an
applet to communicate with the web server as the front end of a 3 tier application. Eventually, I will want to query the web server based on some user event and display the returned data.
Why Doesn't the getFile() call work in actionPerformed()? The text "Button Pushed" shows up in the status bar so I know actionPerformed() was called!
import java.io.* ;
import java.net.* ;
import java.applet.* ;
import java.awt.* ;
import java.awt.event.*;
public class HelloServerFile extends Applet implements ActionListener {
Panel pnlCenter = new Panel(new GridLayout(10,1));
Label lblStatus = new Label("Status:");
public void init() {
Panel pnlNorth = new Panel(new FlowLayout());
Panel pnlSouth = new Panel(new FlowLayout());
Button myButton = new Button("Get File");
setLayout( new BorderLayout() );
myButton.addActionListener(this);
pnlNorth.add(new Label("Version 1.07"));
pnlNorth.add(myButton);
pnlNorth.setBackground(Color.CYAN);
lblStatus.setAlignment(Label.LEFT);
pnlSouth.add(lblStatus);
pnlSouth.setBackground(Color.CYAN);
add(pnlNorth, BorderLayout.NORTH);
add(pnlCenter, BorderLayout.CENTER);
add(pnlSouth, BorderLayout.SOUTH);
getFile();
} // close init
private void getFile() {
String line = new String("Button Pressed.");
try{
URL textURL = new URL( getCodeBase() , "test.txt" );
BufferedReader theStream =
new BufferedReader(new InputStreamReader(textURL.openStream()));
while ( ( line = theStream.readLine() ) != null ) {
pnlCenter.add( new Label( line, Label.CENTER ) );
} // close while loop
theStream.close();
} // close try
catch ( Exception e ) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent ae) {
lblStatus.setText("Button Pushed");
getFile();
} // close actionPerformed
} // close applet