2

Does somebody knows how to detect if user selected cancel button or root disk in java.awt.FileDialog in Mac OS (10.6 - Snow Leopard)????

I have the below code:

System.setProperty("apple.awt.fileDialogForDirectories", "true"); 
FileDialog fd = new FileDialog(this);  
fd.setDirectory(_projectsBaseDir.getPath());  
fd.setLocation(50,50); fd.setVisible(true);  
File selectedFile = new File(fd.getFile()); 
System.setProperty("apple.awt.fileDialogForDirectories", "false");

But if user selects primary disk on the left panel (below Devices), the selection returns null, I cannot diferentiate if user selected primary disk or presssed the cancel button. (both actions return null).

Charles
  • 50,943
  • 13
  • 104
  • 142
user967580
  • 21
  • 3

1 Answers1

0

If it's possible to use Swing, I'd highly recommend using JFileChooser. Then your code would look like this:

JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(_projectsBaseDir.getPath());
fc.setLocation(50,50);
int ret = fc.showOpenDialog(this); // Use .showSaveDialog(this) for save dialog
if(ret == JFileChooser.APPROVE_OPTION)
    File selectedFile = fc.getSelectedFile();

Hope this helps.

Max Feldkamp
  • 527
  • 1
  • 5
  • 16