Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

display error message on applet

Posted on 2004-04-07
3
Medium Priority
?
244 Views
Last Modified: 2010-03-31
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

0
Comment
Question by:buglouie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1500 total points
ID: 10778307
a. you should be doing loadFile in a separate thread
b. you should make loadFile be boolean then
c. you can do if (!loadFile) ....... (set a label text to show error result)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10778334
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10778416
8-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question