Use the result of the appropriate showDialog
method to determine which button was clicked
JFileChooser input = new JFileChooser();
int result = input.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
createFile();
} else if (result == JFileChooser.CANCEL_OPTION) {
System.out.println("Cancel was selected");
}
Note: Clicking the X
button on the JFileChooser
dialog also triggers the CANCEL_OPTION
.