Im trying to write an applet with JFileChooser, so the user can browse the harddrive and click on a file as input

I have an applet written that allows the user to type the input, the input is then used to parse a file.  I need to have the user to be able to browse for a file on the hard drive, and this then to be used again as input to be parsed. When I try this I get various security issues. Would it be possible for  someone to give me some advice on how to go about this.

Thankyou.


import java.awt.*;

//-------------------------------------------------------------------
//  Main applet class

public class FileApplet extends java.applet.Applet {

  public void init() {
   
     add(new Button("Load File"));
 
  }//init

  public boolean action(Event x,Object arg) {
   
     if (x.target instanceof Button && ((String)arg).equals("Load File")) {
            ParsePDB pPDB = new ParsePDB();
            pPDB.other();
     }//if
     return true;
  }//action

  //  These methods allow the applet to also run as an application.

  public static void main(String args[]) {
   
            ParseFile pFILE = new ParseFile();
            pFILE.another();
     
  }//main
 
}//Run
petedeAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
objectsConnect With a Mentor Commented:
If you don't have a cert for signing then the other option is to modify your jre's security policy file.
0
 
CEHJCommented:
It's quite simple: if you want to be able to give an applet that does this to other people, it has to be signed. Applets are not allowed to read from the local disk otherwise.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
petedeAuthor Commented:
How do I go about changing the present applet to one that browses for files on the harddrive?
0
 
girionisCommented:
 Given that your applet will have permissions to browser user's local drive you can have a look here: http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html for how to use a JFileChooser.
0
 
CEHJCommented:
By signing it!
0
 
girionisCommented:
 And another link for signing applets: http://java.sun.com/products/plugin/1.2/docs/signed.html
0
 
petedeAuthor Commented:
That would solve the applets security issue but, could someone help me update the code, from typed user input to user input through browsing from a file?
0
 
petedeAuthor Commented:
That would solve the applets security issue but, could someone help me update the code, from typed user input to user input through browsing from a file?
0
 
CEHJCommented:
Please post the code for typed user input
0
 
petedeAuthor Commented:
The code above, calls the applet input box which then checks the string and verifies the file.

The ParseFile refers to this applet with one line of code

String file = JOptionPane.showInputDialog("Load file:");


import java.awt.*;

//-------------------------------------------------------------------
//  Main applet class

public class FileApplet extends java.applet.Applet {

 public void init() {
   
    add(new Button("Load File"));
 
 }//init

 public boolean action(Event x,Object arg) {
   
    if (x.target instanceof Button && ((String)arg).equals("Load File")) {
           ParseFile pFile = new ParseFile();
           pFile.other();
    }//if
    return true;
 }//action

 //  These methods allow the applet to also run as an application.

 public static void main(String args[]) {
   
           ParseFile pFILE = new ParseFile();
           pFILE.another();
   
 }//main
 
}//Run
0
 
CEHJCommented:
Are you having this as a Swing applet? - i notice JOptionPane somewhere.
0
 
CEHJCommented:
And what is ParseFile?
0
 
petedeAuthor Commented:
Yes, I'm using swing in the ParseFile,
Importing
import java.io.*;
import javax.swing.*;

The ParseFile is where the user input is sent to and the file is processed.

0
 
CEHJCommented:
Can you post the code for ParseFile?
0
 
petedeAuthor Commented:
Yes, I'm using swing in the ParseFile,
Importing
import java.io.*;
import javax.swing.*;

The ParseFile is where the user input is sent to and the file is processed.

0
 
petedeAuthor Commented:
I could email you the File
0
 
petedeAuthor Commented:
I could email you the File
0
 
petedeAuthor Commented:
I could email you the File
0
 
CEHJCommented:
Probably better if you juust link up the code. If i give you a String containing a file name, will you be able to link it up to your code?
0
 
petedeAuthor Commented:
I can try...
Thanks
0
 
CEHJCommented:
Actually it would be more flexible if you returned a file:


File getFile() {
  JFileChooser fc = new JFileChooser();
  return fc.getSelectedFile();
}
0
 
petedeAuthor Commented:
My email is idjohndoe@hotmail.com,
If you can help anymore with this I wpuld appreaciate it...
0
 
CEHJCommented:
Well, in the terms of this site, we have to confine ourselves to the site itself, otherwise other people can't benefit in future from the solutions.

Essentially, the code above will allow a file to be opened and a reference to that file to be returned from a JFileChooser (if the applet is signed).

It just needs to be called. Are you OK with that?
0
 
objectsCommented:
import java.awt.*;
import javax.swing.*;

//-------------------------------------------------------------------
//  Main applet class

public class FileApplet extends JApplet
implements ActionListener {
private static JFileChooser chooser = new JFileChooser();
private JButton load = new JButton("Load File");

 public void init() {
   
    getContentPane().add(load);
    load.addActionListener(this);
 }//init

 public void actionPerformed(ActionEvent e) {
    int ret = chooser.showOpenDialog(this);
    if (ret==JFileChooser.APPROVE_OPTION)
    {
       File f = chooser.getSelectedFile();
       // do what is required with file
    }
 }//action

 //  These methods allow the applet to also run as an application.

 public static void main(String args[]) {
    int ret = chooser.showOpenDialog(this);
    if (ret==JFileChooser.APPROVE_OPTION)
    {
       File f = chooser.getSelectedFile();
       // do what is required with file
    }
 }//main
 
}//Run
0
 
CEHJCommented:
Unless either

a. You are the only person using your program
b. Your users are patient computer (perferably Java) nerds

then altering policy files is not practicable though.
0
 
objectsCommented:
And if you don't want to fork out the $ for a cert then signing aint too practical either ;)
0
 
CEHJCommented:
That's also true.
0
 
petedeAuthor Commented:
Many thanks for all the advice, there are only a few users of the application. So I guess the best option would be to to sign the applet.

0
 
objectsCommented:
> there are only a few users of the application.
> So I guess the best option would be to to sign the applet.

No it's probably easier to update the security policy file on each of the machines if there is aonly a few of them.
Unless you already have digital certificate.


0
 
CEHJCommented:
You need to evaluate the following:

1. Assess the costs and trouble of getting a certificate
2. Assess the trouble of reconfiguring each user machine and ask yourself if this is practicable for your users.
0
 
objectsCommented:
> 1. Assess the costs and trouble of getting a certificate

About $400 i think

> 2. Assess the trouble of reconfiguring each user machine
> and ask yourself if this is practicable for your users.

This is simply adding an entry to a file, so if it is only a few machines then no big drama.

Another option would be to distribute the code to them as an application instead of as an applet.
0
 
petedeAuthor Commented:
Thanks Guys,
I appreicate the advice
0
All Courses

From novice to tech pro — start learning today.