0

I have developed simple notepad application in java . When I want to save file I want to add .txt extension to that file. This is working fine in file dialog

String filename;
FileDialog fd = new FileDialog(Editor.this,"Save File",FileDialog.SAVE);
fd.show();
if (fd.getFile()!=null)
{
    filename = fd.getDirectory() + fd.getFile()+".txt";
    setTitle(filename);
}

It's working fine in the FileDialog with finename.txt

But when I take

String filename;
JFileChooser save = new JFilechooser();

Try to add extension

filename =save.getDirectory() + save.getFile()+".txt";

It's not working how can i solve this issue?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • If you do not give any sample input, the output you expect and what you get instead, nobody will be able to help – fge Jun 15 '13 at 09:42
  • Is it me or does JFileChooser not even have getDirectory() and getFile() methods? – Davey Chu Jun 15 '13 at 09:58
  • here i am trying to open a save dialog and trying to save the file with extension of .txt how can i achieve its working fine in filechooser but not working in Jfilechooser? – user2488543 Jun 15 '13 at 10:15
  • @daveychu `JFileChooser` has [`getCurrentDirectory()`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getCurrentDirectory%28%29) & [`getSelectedFile()`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#getSelectedFile%28%29). – Andrew Thompson Jun 15 '13 at 10:56
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jun 15 '13 at 10:57
  • @AndrewThompson I was just wondering why topic starter posted non-compiling code. – Davey Chu Jun 15 '13 at 14:34
  • @daveychu The question does flip between the AWT & Swing versions of a file chooser. Have to agree it is a little confusing though. – Andrew Thompson Jun 15 '13 at 15:41

1 Answers1

2

In JFileChooser to get the file you use:

File currentFile= save.getCurrentFile();

and only then you get what you need (for example the path):

String fileName = file.getPath();

On the side note, JFileChooser has a nice documentation, make sure you read it: http://docs.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html

darijan
  • 9,725
  • 25
  • 38