Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JFile chooser default file name setting

843806Nov 28 2007 — edited Nov 28 2007
Could anybody pls tell me how I can set a default file name when JFile chooser Save dialog box appears so that the user doesn't need to name the file to be saved. And I also want to get the name of a file from current selected directory so that I can warn user for the replacement, The code I tried with is as follows
public void SaveAs()
	{
                String FileName="";

		JFileChooser jfc = new JFileChooser();
		int r = jfc.showSaveDialog(this);
						
		if (r== JFileChooser.APPROVE_OPTION)
		{
			FileName = jfc.getSelectedFile().getPath();
			FileName=FileName+".java";
			writeFile(FileName);
			
		}

	}//End of SaveAs() Method

  
  
  public void writeFile(String fn)
	{
           String contetnt="THIS IS A FILE";
		try{

			FileWriter fw = new FileWriter(fn);

			fw.write(contetnt);

			fw.close();


		}catch(FileNotFoundException fnfe){}
		 catch(IOException ioe){}

	}//End of writeFile(String fn) Method
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 26 2007
Added on Nov 28 2007
3 comments
284 views