JFileChooser.showSaveDialog: All files greyed out
Asked Answered
L

3

6

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)

Lignite answered 22/2, 2013 at 3:8 Comment(6)
Short answer, yes, this is expected behaviour. Unlike Windows, which would simply exclude the files from the visible list, for some strange reason the Mac OS Look&Feel decides that the files should be greyed out (when you can't select them)Leonor
But according to the filter, .txt files should be both visible + selectable (see how they look in the Open Dialog: in a black (as opposed to grey) font.Lignite
@Leonor is correct. You can select a grayed out name, which then becomes the new candidate Save As name.Rape
@Rape Right - all the files are selectable. What I'm looking for is the .txt files to be displayed in the "normal" color while in the save dialog, the same way they look in the open dialogLignite
The default behavior is not unexpected by Mac users, but I've suggested some alternatives below.Rape
@Rape (+ @MadProgrammer) - Yeah, looking at other native apps it seems like that is the indeed the case, although I've never really noticed it before. I guess to my eyes it looked particularly bad in the JFileChooser. I'll play around with L+F if it keeps bugging me :)Lignite
R
2

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.

Rape answered 22/2, 2013 at 3:44 Comment(1)
Ooh, nice link. ;) OTOH I suspect the typical OS X user would prefer the default look ('path of least surprise' etc.). Nice to have alternatives though. :)Shaveling
Z
2

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.

Zilpah answered 19/8, 2015 at 20:37 Comment(0)
P
0

Mmm... I think, that show dialogs the way you do is not the best way

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

I think that could be generating a conflict. Why don't you try to use a JFrame to help you? Try with this piece of code, just to know if the problem is the 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());
}

As a matter of fact, you could try using the setLookAndFeel, I remember I had this issue working with my Macbook Pro.

Plotinus answered 22/2, 2013 at 3:17 Comment(1)
Using a parent frame gives me the same behavoirLignite

© 2022 - 2024 — McMap. All rights reserved.