Hi there,

I have got this problem which is rather puzzling, to me at least.

My team-mate and I each did our coding and they work as standalone. His part is the UI and mine the backend processing.

He did his code using BlueJ using JDK 1.3, I did mine in JCreator using JDK 1.4.2_8. (I know its bad to not use the same version... :o but please read on.)

Apparently when compiling his code using BlueJ on my pc (WinXP), there were some compilation errors, and I fixed them. (Most prob due to different JDK versions. The error was in his type casting.) Compiled them again as standalone on my pc and they worked fine once more when i run them.

Now I tried to run his applet code with my code integrated, through BlueJ, by just integrating one simple function as a test. The applet opens and loads up. All the UI appears. Tried the function but cannot work. :eek:

Next I tried using JCreator to compile his code with mine, it compiled fine. Then I created a html file with the <applet> tags. It opens up the applet as before with all the UI buttons loaded but the integrated function cannot work. :eek:

Next I used the commands at command prompt:
appletviewer sample.htm
The applet opens up and works fine. The integrated command is working. :!:

The integrated command is opening a textfile and retrieve and store its contents into an arraylist. Upon user clicking a button on the UI, it would seek out the a particular cell of contents in the arraylist.

I did some debugging for the failed scenarios and discovered that my code could not find my myTextFile.txt file when it exists on the same directory. Tried to convert file type from unix to windows, still have the same problem. :cry:

Could someone please enlighten me on how to go about solving this problem please...

Very sincerely,
Rackus

Code:
UI Snipplet:
========


public class UIFILE extends JApplet implements MouseListener, ActionListener, ItemListener
{  .... ... //declaration of global variables
private TextArea TA = new TextArea (5,10);
myClassFile cs = new myClassFile;
... ... ...


public void actionPerformed ( ActionEvent e ) {
Object source = e.getSource();
... ...
if (source == getContent ) {
TA.append("Contents: "+cs.retrieveDetails("XXX"));
}
... ...
}
... ... ...
}



BackEndProcessing Snipplet:
=====================


pulic class myClassFile {


.....
//declaration of my global variables
readFile rf = new readFile();
..... ....


public myClassFile()
{
rf.openNstore(); //opens the txt file and stores it into an array list. as standalone it works.
}
... ...
public String retrieveDetails( String toCheckAgainst )
{
String result = rf.getValue(toCheckAgainst); //it gets the value. works as standalone.
return result;
}
... .... ...
}

Recommended Answers

All 9 Replies

Well, I can't see anything from your code.. nor can I test it.

But my best guest is that you haven't actually given focus to the applet when you load it in an html page. When you load it in appletviewer, it is given focus (as it is obviously the only thing running). This is not the case in an html browser (because other items on the page may want focus or the browser simply doesn't give the applet focus for security reasons).

You have possibly tried clicking on the applet then seeing if it accepts user input events?

Hi alpha_foobar,

I can't post up my whole code coz its over 20 class files. :o (unless you dun mind... )

As for giving focus, I don't really get you.

As for clicking on the applet to detect user inputs, yes, it works. but just in the backend makes no sense.

The applet in html is able to detect user input events. When I modify the code in actionPerformed(), to display a test message in a textarea after each button is clicked before attempting to call a function in my set of the code, it is able to display the test message.

E.g. I got 5 buttons. Upon clicking each button it will display "Going to retrieveDetails("XXX") now..." in the textarea. Then it calls that method and returns null. The problem is that it is not suppose to return null but a string instead.

The reason why it is returning null is because it can't find a certain textfile in the directory. But I made sure that the textfile exists. I checked the name and extension of the file. It is correct.

I checked the bufferedreader code to open that file to read in stuff. Its also correct.

Anyone able to help??

it could just be cached...
open control panel
double click java
under "general" --> temporary internet files click delete files
make sure "downloaded applets" is checked
click ok, click ok

now try loading your applet again

ahhh.. my bad i overlooked that part about reading a textfile.... on which computer is the file stored and how are you trying to read the file?

sandboxing of applets prevents them from opening files on your computer... what is the code that you are using to open the file... The UI code probably has nothing to do with this.

-- and i hadn't read the above post when i posted.

Hi paradox814 and alpha_foobar,

paradox814:

I found a "Java-Plugin" in Control Panel, but can't find a General tab. (I'm using 1.4.2_8). So I went to "Internet Options" to delete all my cookies and offline files. Tried.. but the applet can't work. :sad: Thanks though for trying to help me out.

alpha_foobar:
Currently, the textfile is stored on my computer (OS: winXP; a local system). If we ever get it all working, it would be applet running on client browser and retrieving the details from a server.

Sandboxing... hmm... did not do anything specifically sandboxing. :confused:

The code below works when not integrated with the UI. As in if I were to put print statements to check the retrieved values, the assgined values in an arraylist, they are all working as expected prior to integrating.


Help help...

public class myFileReader{

private String myFile = "myFile.txt"; 


  public fileReader()
  {  
    readFile(myFile);
  }

  private void readFile(String filename)
  {
    String line;
    System.out.println("TEST filename:"+filename);
    try
    {
      BufferedReader buffer = new BufferedReader(new FileReader(filename));
      while ((line = buffer.readLine()) != null) 
      {
        storeLine(line);
      }
      buffer.close();
    }
    catch (Exception e)
    { 
      System.out.println("Exception occurred: /n" +e); 
      System.exit(0);
    }
  }

}

Sandboxing refers to the security levels of Java Applets...

Typically a Java Applet is downloaded from the internet, and so it would be a security risk if the applet could access arbitrary files on the client computer. It can however access arbitrary files from a server.

The 'sandbox' that a java applet runs in is supposed to symbolise an environment on a client computer, that can be played in without causing much of a mess. It is possible to get around sandboxing with signed code.

check out this link: http://www.securingjava.com/chapter-three/

If you will have the txt file on a server, use a URL stream to get the file. Then use the full URL to the file.

Hi alpha_foobar,

Thank you for the links for sandboxing. I understand better now what is sandboxing.

But is there a way to get it to work locally, by running the applet through a html file on a local computer system with all the files in the same directory instead of using the appletviewer? The current setup i have is everything stored in the same directory on my pc.

Is it only possible for me to test it through a web browser via a html file only after the server is setup and I access the applet via a web browser on a client system?

Thank you once again for your url links about sandboxing. I really appreciate it. :)

Very sincerely,
rackus

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.