?
Solved

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

Posted on 2003-03-12
33
Medium Priority
?
261 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:petede
[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
  • 13
  • 13
  • 5
  • +1
33 Comments
 
LVL 86

Expert Comment

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

Expert Comment

by:CEHJ
ID: 8119177
0
 

Author Comment

by:petede
ID: 8119261
How do I go about changing the present applet to one that browses for files on the harddrive?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 35

Expert Comment

by:girionis
ID: 8119300
 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8119304
By signing it!
0
 
LVL 35

Expert Comment

by:girionis
ID: 8119316
 And another link for signing applets: http://java.sun.com/products/plugin/1.2/docs/signed.html
0
 

Author Comment

by:petede
ID: 8119804
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
 

Author Comment

by:petede
ID: 8119916
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8119935
Please post the code for typed user input
0
 

Author Comment

by:petede
ID: 8120047
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8120112
Are you having this as a Swing applet? - i notice JOptionPane somewhere.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8120122
And what is ParseFile?
0
 

Author Comment

by:petede
ID: 8120158
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8120182
Can you post the code for ParseFile?
0
 

Author Comment

by:petede
ID: 8120197
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
 

Author Comment

by:petede
ID: 8120210
I could email you the File
0
 

Author Comment

by:petede
ID: 8120258
I could email you the File
0
 

Author Comment

by:petede
ID: 8120260
I could email you the File
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8120399
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
 

Author Comment

by:petede
ID: 8120456
I can try...
Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8120613
Actually it would be more flexible if you returned a file:


File getFile() {
  JFileChooser fc = new JFileChooser();
  return fc.getSelectedFile();
}
0
 

Author Comment

by:petede
ID: 8120773
My email is idjohndoe@hotmail.com,
If you can help anymore with this I wpuld appreaciate it...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8121014
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
 
LVL 92

Expert Comment

by:objects
ID: 8123316
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
 
LVL 92

Accepted Solution

by:
objects earned 1500 total points
ID: 8123321
If you don't have a cert for signing then the other option is to modify your jre's security policy file.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123365
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
 
LVL 92

Expert Comment

by:objects
ID: 8123400
And if you don't want to fork out the $ for a cert then signing aint too practical either ;)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8123430
That's also true.
0
 

Author Comment

by:petede
ID: 8127064
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
 
LVL 92

Expert Comment

by:objects
ID: 8132725
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8135346
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
 
LVL 92

Expert Comment

by:objects
ID: 8139704
> 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
 

Author Comment

by:petede
ID: 8166662
Thanks Guys,
I appreicate the advice
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month7 days, 20 hours left to enroll

765 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