0

I need to use a JFileChooser in my simple Quiz game to load and save 'Question /Answer' sets , when the FileChooser window opens i want it to be in the current working directory, i want this to work in Linux,Windows and OSX, i am doubtful if the "user.dir" mehtod works in all platforms , Can someone help out , Code Below...

For loading

public class openMenuItemListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
nextButton.setEnabled(true);
JFileChooser fileChooser=new JFileChooser();
fileChooser.showOpenDialog(frame);
loadFile(fileChooser.getSelectedFile());
}
}

For Saving:

class saveMenuListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    JFileChooser fileSave=new JFileChooser();
    fileSave.showSaveDialog(frame);
    saveFile(fileSave.getSelectedFile());

    }
    }//class ends 
Sainath S.R
  • 3,074
  • 9
  • 41
  • 72
  • 1
    *"I need to use a JFileChooser in my simple Quiz game to load and save 'Question /Answer' sets , when the FileChooser window opens i want it to be in the current working directory, i want this to work in Linux,Windows and OSX, i am doubtful if the "user.dir" mehtod works in all platforms"* It won't *ever* work for *any* OS for *any* application launched using Java Web Start, which would be the usual way that desktop apps. are distributed. In those apps. the directory of the app. (where it is installed) is deliberately hidden from the app. itself. .. – Andrew Thompson Oct 20 '14 at 10:24
  • .. What form exactly are these quizzes stored in (text, HTML, serialized `String` objects..)? – Andrew Thompson Oct 20 '14 at 10:25
  • *"its working now thanks"* It's easy enough to get many things working on a single development machine. It is at time of deployment to anyone else that you'll then need to deal with problems like I was describing. But then, why is the app. offering a `JFileChooser` as opposed to simply a `JList` of available quizzes? Do you supply all the quizzes, or can the end user design new ones? – Andrew Thompson Oct 20 '14 at 10:34
  • @AndrewThompson I have two applicatios, a quiz builder to build a quiz ,save as text file , then load and play it from a quizplayer , its just a toy application I'm using as I'm learning java io,nothing much – Sainath S.R Oct 20 '14 at 10:42
  • @AndrewThompson I think you are mislead, i dont have any installers ,.jar files , web deployement etc , no need to access folders etc, i have just 2 .java Files which i run from the terminal ,Its that Simple!,Anyway thanks for the useful Hints – Sainath S.R Oct 20 '14 at 10:45

1 Answers1

1

This gets you a `JFileChooser' referencing the current working directory (i.e. application startup location) on any system:

new JFileChooser(".");
jhkuperus
  • 1,439
  • 11
  • 15