Link to home
Start Free TrialLog in
Avatar of buglouie
buglouie

asked on

display error message on applet

Trying to display error message on applet if wrong text file name is typed in the myTxtBox

My code so far:-



//<APPLET code=Lou2.class width=400 height=200> </APPLET>


import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class Lou2 extends Applet implements ActionListener {
     Label myLabel;
     TextArea myBox;
     TextField myTxtbox; //Added new textbox onto applet into which file name can be typed for 23a.
     Button mybtn;
     public void init() {
          myLabel = new Label(
          "The text area shows the information read from the file on the server");
          myBox = new TextArea("", 4, 20);
          myTxtbox = new TextField(20);
          myTxtbox.setText("readthis.txt");
          mybtn = new Button("Load File into Text Area");
          add(myLabel);
          add(myBox);
          add(myTxtbox);
          add(mybtn);
          mybtn.addActionListener(this);
     } // end of init()
     public void actionPerformed(ActionEvent e) {
          loadFile();
     }//end action performed
     public void loadFile() {
          try {
               URL myURL = new URL(getDocumentBase(), myTxtbox.getText());
               // open file on server
               URLConnection myConn = myURL.openConnection();
               InputStreamReader isr = new InputStreamReader(myConn
                         .getInputStream());
               BufferedReader bufr = new BufferedReader(isr);
               String line;
               line = bufr.readLine(); // read ahead  
               while (line != null) {
                    myBox.append(line + "\n");
                    line = bufr.readLine();
               } // end of while          
          } // end of try
          catch (MalformedURLException me) {
               System.out.println("Bad URL encountered" + me);
          } catch (IOException ioe) {
               System.out.println("IO Exception" + ioe);

          }
     }
} // end of Applet

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
public void actionPerformed(ActionEvent e) {
      Thread fileLoader = new Thread(new Runnable() {
            public void run() {
                  if (!loadFile()) {
                        label.setText("Fileload failed");
                  }
            }    
      });
      fileLoader.start()

}//end action performed
8-)