This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File name in JDialog

 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Vijay
 
Tapan Maru
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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);
}



//PLEASE CORRECT COMPILATION ERROR AND TYPO CORRECTIONS SPECIALLY IN BRACKETS IN IF CONDITION ABOVE
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic