Supereme
Supereme

Reputation: 2409

How to get path to save a file from user?

I'm developing an application in which I want to create an 'MS-EXCEL' file and for that I want the name for the file to be given by user and later on I want to provide an option to the user to save at the path where it wants. I know about JFileChooser but it only allows to select a file from the existing files whereas I want to save. What can be the way? Please give me some brief description.

Upvotes: 1

Views: 10956

Answers (5)

willcodejavaforfood
willcodejavaforfood

Reputation: 44063

    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int option = chooser.showSaveDialog(null);
    if (option == JFileChooser.APPROVE_OPTION)
    {
        // do soemthing
    }

Upvotes: 2

stacker
stacker

Reputation: 68942

A full example on FileChooser you find here

Upvotes: 2

Ash
Ash

Reputation: 9426

You can use JFileChooser.setFileSelectionMode to allow selection of either files, directories, or both files and directories.

You can also type in the name of a file that doesn't already exist.

Upvotes: 2

Robert
Robert

Reputation: 1845

Is there a JDirectoryChooser. Sounds to easy!

Upvotes: 1

Itay Maman
Itay Maman

Reputation: 30723

Have you tried the JFileChooser's showSaveDialog() method?

Upvotes: 1

Related Questions