• Post Reply Bookmark Topic Watch Topic
  • New Topic

Classing whitespace as invalid name when close button is pressed  RSS feed

 
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a filechooser that works how it should and if anyone enters anything with a dot that isnt .xml it shows an invalid file name message. However when I dont choose a file and press cancel it still says that because when my boolean hits false it's the first thing it hits in that section of code.



If I want it to just close down without it saying anything is there sort of if statement I could do that would prevent this? But if it is an invalid file name it will still show that message?

Looked at the Oracle stuff already didn't seem to really help with what I wanted?
 
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call JFileChooser.showSaveDialog(..) it returns an int which can have one of three values: JFileChooser.APPROVE_OPTION, CANCEL_OPTION, or ERROR_OPTION. You can use an if statement to check if the approve option was selected.

Javadoc is your friend: http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I put that in my code like this in line 3:

 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically yes, but you'd want to have code that does something when the condition is met. The ; at the end of the if statement means end of statement - you need to remove that and add an action (like showing a messagebox).

I don't know where the writeSuccess variable comes from or why it's set. You might have to play with the placement of your check if the user pressed cancel to make sure it's appropriate for your code.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just trying to edit my message - I went back and looked at the Oracle example and after playing around with it a little it works correctly in my code - thank you for taking the time to help and by prompting me to go back to the site
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!