This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes File name in JDialog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "File name in JDialog" Watch "File name in JDialog" New topic

File name in JDialog

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 186

Dear all,

I have a problem with JDialog. I have a File->Open functionality in the Swing utility i'm developing. Whenever i open a JDialog box appears to the user to select the file and after he selects the file from the particular folder, the file name appears in the File Name textbox.

Suppose if the user tampers the file name before he clicks Open button in the JDialog box, how can i capture it and display a message to the user that the requested file is invalid.

Now, the utility is opening the file and also creates an extra new file in the folder with the tampered file name

Kindly provide your help.

Thanks & Regards,
Tapan Maru
Ranch Hand

Joined: May 08, 2006
Posts: 65
//You will get a selected file name from the following method
File selectedFile = fileChooser.getSelectedFile();
//You will get a directory name from the following method
File currentDirectory = fileChooser.getCurrentDirectory();

// now check for the file in that directory

if (!new File(currentDirectory.getName()+selectedFile.getName())).exists()) {
JOptionPane.showMessageDialog(this, "File Not Found", "Error", JOptionPane.ERROR_MESSAGE);


Tapan Maru
I agree. Here's the link:
subject: File name in JDialog
It's not a secret anymore!