Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file dialog

 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we provide the sample code just like file tag in html.
ie. we have browse button and when u select the browse button u can select the file i your computer or create one file in specified directory in one computer.i have used filechooser.but unable toperform the operation just like file tag in html
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I fully understand your request. Did you check out the Java tutorial section on JFileChooser? It contains information on how to display the appropriate type of dialog. You can configure it for Open, Save, directories or files only, and so on.

You only really use JFileChooser to get File instances using the getSelectedFile() method. Once you have this you can find the path of the selected file and then take the appropriate action (create it if the action is a save; open the file and read it for loading; just display the path as a string for information, etc).
 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using JFileChooser we can retreive a file by using getSelectedFile().
like that can we create one file under specified directory.if so tellme by which we can do this.i have tried .but no method haven't seen
 
Ko Wey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect you want your JFileChooser only to select directories,
then you can make a new file in the selected directory?

Very roughly, something like this:

JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(component);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File directory = chooser.getSelectedFile();
File babyFile = new File(directory,"name_of_babyfile);
};
//etc....

of course baby-file does NOT exist: you still have to create it on disk
with FileWriter or FileOutputStream....

Hope I helped you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic