开发者

How to detect if user pressed cancel button or selected root (primary disk) with java.awt.FileDialog in MAC OS?

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 开发者_StackOverflow中文版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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜