tags:

views:

596

answers:

2

Hi, I am new to Swing and wish to implement the download file feature in my Swing code, that would allow the user to either save or open the specific file.

I did have a look at JFileChooser.showOpenDialog and showSaveDialog, but I don't wish to use it, as it gives me the option to choose any file from the file system.

Hope my problem is clear. Please help me with this.

+5  A: 

You want use them, and add a filter. For Example:

    JFileChooser chooser = new JFileChooser();
    // Note: source for ExampleFileFilter can be found in FileChooserDemo,
    // under the demo/jfc directory in the Java 2 SDK, Standard Edition.
    ExampleFileFilter filter = new ExampleFileFilter();
    filter.addExtension("jpg");
    filter.setDescription("JPG & GIF Images");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showSaveDialog(parent);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       System.out.println("You chose to open this file: " +
            chooser.getSelectedFile().getName());
    }

this will only show JPG and GIF files. Example stolen from here

Edit: Just so you know ExampleFileFilter implements the abstact class FileFilter

Edit: Since you know the name of the file, you could just have a button that says open and use Runtime.getRuntime.exec('the file to be opened.doc") and that should open it in the appropriate application.

For saving you will still want to prompt them to find out where they want to save it so you would still need the JFileChooser. I would still use a filter, and determine what the file extension will be dynamically if necessary and then do:

    JFileChooser chooser = new JFileChooser();
    // Note: source for ExampleFileFilter can be found in FileChooserDemo,
    // under the demo/jfc directory in the Java 2 SDK, Standard Edition.

    String selectedFile = "The suggested save name.";
    chooser.setSelectedFile(selectedFile);

    ExampleFileFilter filter = new ExampleFileFilter();
    String extension = "Do something to find your extension";
    filter.addExtension(extension);
    filter.setDescription("JPG & GIF Images");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showSaveDialog(parent);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       System.out.println("You chose to open this file: " +
            chooser.getSelectedFile().getName());
       //then write your code to write to disk
    }

Hope that helps.

jschoen
Hey, I dont want to put restrictions for openign a file but want to have a dialog box that would allow the user to either open or save a particular file, say a report that is generated by my system.Hope it is much clear now.
NewToJava
Are you saying that you want to be able to prompt them to open a specific file that you already know the name of?
jschoen
yes u r absolutly rit..wat i want is if we have an atatchment on gmail, the way it prompts download and then shows either save or open.
NewToJava
Hey Thanks.. not tried it out.. but i think this shud work fine for me :)
NewToJava
+1  A: 

You can add filter to file chooser. It's easy to implement your own which will accept only the files that you allow.

After choosing file you need to implement saving / reading of file by yourself. There is no such thing as download/upload in desktop applications.

Dev er dev
as far as i understand a filter wud just help me to restrict some file types. what i want is a dialog that wud allow the user to either savor open a particular file generated by my tool say a report or smthing.
NewToJava
Thnaks Marko.. now i have an idea how its to be done.. thanks a lot for ur inputs :)
NewToJava
An upvote is as good as thanks :)
Dev er dev