JFileChooser.showSaveDialog: all files are greyed out

I am trying to use JFileChooserto get files to load and save. The dialog with openFileDialog()works fine, but when I use the method saveFileDialog(), the dialog box contains all the file names that are grayed out. This happens with or without FileFilter(my example includes one to better show what I see).

Here is a minimal program to illustrate:

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Temp extends JFrame {
    public static void main(String[] args){
    JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Text File", "txt");
        chooser.setFileFilter(filter);

        frame.setVisible(true);
        chooser.showOpenDialog(null);
        chooser.showSaveDialog(null);
    }
}

Here is what I see in the Open dialog box: Open the dialog

NrpzW.png

Here is what I see in the Save dialog : Save dialog

q31zX.png

Although you are inactive, all files in the save dialog can be selected.

I am on Mac / Mountain Lion and Java 7, if that matters.

? ?

(: MadProgrammer + trashgod , , , () Mac)

+5
3

.txt, "" , .

FileChooserUI, Look and Feel, . AquaFileChooserUI Mac OS X. L & F ( ) FileChooserUI File Browser.

+2

:

JFileChooser chooser = new JFileChooser(...);
chooser.showDialog(myFrame, "Save");

, FileFilter , .

+1

... , , ,

chooser.showOpenDialog(null);
        chooser.showSaveDialog(null);

I think this can cause a conflict. Why aren't you trying to use JFrame to help you? Try this piece of code just to find out if the problem is saveDialog. Myabe, then you can adapt it to your programming requirements.

JFrame parentFrame = new JFrame();

JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Specify a file to save");    

int userSelection = fileChooser.showSaveDialog(parentFrame);

if (userSelection == JFileChooser.APPROVE_OPTION) {
    File fileToSave = fileChooser.getSelectedFile();
    System.out.println("Save as file: " + fileToSave.getAbsolutePath());
}

Actually you can try using setLookAndFeel, I remember that I had this problem with my MacBook Pro.

0
source

All Articles