0

Currently using JFileChooser to save a file to the users system. Issue is that the user interface it uses to select the file destination is the ugly Swing UI and not the Windows file explorer UI. Is there an attribute that I can easily change for the JFileChooser.

Below is my code:

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

    int userSelection = fileChooser.showSaveDialog(this);

    if (userSelection == JFileChooser.APPROVE_OPTION) {
        fileToSave = fileChooser.getSelectedFile();
        String filePath = fileToSave.getPath();
        if(!filePath.toLowerCase().endsWith(".csv"))
        {
            fileToSave = new File(filePath + ".csv");
        }
    }

I have also defined fileToSave earlier and the code all works, this is purely a cosmetic issue.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
teamyates
  • 414
  • 1
  • 7
  • 25
  • I investigated this a while ago. AFAIK it is not possible to invoke Windows File Save dialog using Swing, at least not in Java 8 and before. Windows Save As dialog can be invoked using JavaFX though. – david a. Jun 25 '18 at 11:49
  • 3
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Is `UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());` called before the GUI appears? 3) Alternately, you might look into creating a custom file chooser UI as seen in the [File Browser GUI](http://codereview.stackexchange.com/q/4446/7784). – Andrew Thompson Jun 25 '18 at 11:50
  • .. 4) Here is a [screen shot of the Windows PLAF file chooser](https://stackoverflow.com/a/17630202/418556). – Andrew Thompson Jun 25 '18 at 11:55

1 Answers1

2

As Andrew Thompson mentioned, UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); is the method you are looking for.

Try this:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //set Look and Feel to Windows
JFileChooser fileChooser = new JFileChooser(); //Create a new GUI that will use the current(windows) Look and Feel
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); //revert the Look and Feel back to the ugly Swing

// rest of the code...
Doga Oruc
  • 783
  • 3
  • 16