Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFileChooser.showSaveDialog: All files greyed out

I'm trying to use the JFileChooser to get files for loading and saving. The dialog that comes up with openFileDialog() works fine, but when I use the saveFileDialog() method, the dialog window has all the file names greyed out. This happens with or without a FileFilter (my example includes one to better show what I'm seeing).

Here's 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's what I see in the Open dialog: Open Dialog

Here's what I see in the Save dialog: Save Dialog

Despite being greyed out, all the files in the save dialog are selectable.

I'm on Mac/Mountain Lion and Java 7 if it matters.

Is this expected behavior? Is there a way to change this?

(Edit: per comments by MadProgrammer + trashgod below, this appears to be consistent with the look + feel of other (native) Mac apps)

like image 532
YosemiteMark Avatar asked Feb 22 '13 03:02

YosemiteMark


2 Answers

I'm looking for the .txt files to be displayed in the "normal" color while in the save dialog.

That's controlled by the FileChooserUI delegate specific to a particular Look & Feel, e.g. AquaFileChooserUI on Mac OS X. You can use a different L&F, (laboriously) write your own FileChooserUI, or develop a custom File Browser GUI.

like image 62
trashgod Avatar answered Oct 17 '22 03:10

trashgod


What I ended up doing was to use:

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

My save dialog looks like a save dialog, and the FileFilter greys out only files that fail its test.

like image 41
Matthew Avatar answered Oct 17 '22 02:10

Matthew