4

Search doesn't turn anything up for this problem.

I'm using simple code to display a JFileChooser dialog with customized title and accept button:

JFileChooser fc = new JFileChooser();
fc.showDialog(null,"MyText");

On Windows 7 this works as expected: a Save Dialog is displayed, with "Save" replaced by "MyText" on the Accept button and dialog title.

However, on Mac OS X, only the Accept button text is changed - the dialog title is blank. I'm using Java SE 7 and MacOS 10.8.5.

By inserting this line between the two above:

fc.setDialogTitle("MyText");

The correct title is displayed. Is this a known issue, and/or can anyone else reproduce this behavior?

ags
  • 719
  • 7
  • 23
  • 1
    java.version = 1.7.0_07 java.vm.version = 23.3-b01 java.runtime.version = 1.7.9_07-b10 – ags Aug 25 '14 at 05:59
  • 1
    Cool. But more importantly, have you tried the advice of icza? Honestly, I was skeptical that it was a bug in Java and did not notice the use of `showDialog(..)` – Andrew Thompson Aug 25 '14 at 06:02

2 Answers2

7

What you experience on Windows is not the expected behaviour (as it is not documented), it is just an implementation side effect.

The showDialog() is used to display a custom dialog (e.g. not an Open nor Save dialog). It has a parameter to specify the text of the Approve button. If the title has not been set with the setDialogTitle() method, the implementation arbitrarily chooses to use the approve button's text as the title on Windows OS, however this is not documented anywhere and you should not count on this to work.

If you want a custom title, use setDialogTitle(). If you want a custom approve button text, use setApproveButtonText(). Obviously showDialog() also takes the approve button's text in which case you do not need to call setApproveButtonText() prior.

If you want an Open dialog, use the showOpenDialog() method. If you want a Save dialog, use the showSaveDialog(). Only use showDialog() if you want a custom dialog.

icza
  • 389,944
  • 63
  • 907
  • 827
  • 3
    And in support the answer (+1), the JavaDocs...[`public int showDialog(Component parent, String approveButtonText)`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#showDialog(java.awt.Component,%20java.lang.String)) – MadProgrammer Aug 25 '14 at 05:54
  • Yes, I do see the API docs do only refer to the approve button text. I was pretty sure I did see it stated somewhere (official) though - and found it in the java tutorials: "The first argument to the showDialog method is the parent component for the dialog. The second argument is a String object that provides both the title for the dialog window and the label for the approve button." @ http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html – ags Aug 25 '14 at 06:07
  • @ags Yes, but if you call `setDialogTitle()` prior to calling `showDialog()`, the dialog title will remain the one you set with the `setDialogTitle()` method. – icza Aug 25 '14 at 06:12
  • Also - in searching before asking the question, I found a discussion here: http://www.coderanch.com/t/536633/java/java/JFileChooser-modification about how the approve button text can't be changed for the Save or Open dialogs. It's from 2011 and apparently has been fixed. I just tested and verified that calling setApproveButtonText() before showOpenDialog(), the desired behavior is exhibited. – ags Aug 25 '14 at 06:23
  • @icza You are correct and your answer reasonable. I actually need a custom dialog - at least the title/button text. Interestingly, I found by experimenting that if showDialog() is called before setApproveButtonText() and then showOpenDialog() is called, the setApproveButtonText() does not take effect. Not that I'd do this, just a strange observation. Thanks. – ags Aug 25 '14 at 06:28
4
  1. here are all accesible keys for UIManager (part of them aren't accessible by looping in UIManager, for all supported Native OS's and standard LaFs), with notice that success is volatille, depends of Native OS and used LaF

  2. you can to get parent from JFileChooser, to cast to JDialog

  3. add JFileChooser to your own JDialog (simple without any special settings, e.g. myDialog.add(myFileChooser); + myDialog.pack();)

  4. there is possible to play with components tree


all accesible keys for UIManager (part of them ....

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Test extends JDialog {

    private JFileChooser fc = null;
    private JFrame bfcParent = null;

    public Test(JFrame parent, boolean modal) {
        super(parent, modal);
        this.bfcParent = parent;
        if (fc == null) {
            fc = new JFileChooser();
            fc.setAcceptAllFileFilterUsed(false);
            fc.setLocale(Locale.ENGLISH);
            UIManager.put("FileChooser.openDialogTitleText", "Open Dialog");
            UIManager.put("FileChooser.saveDialogTitleText", "Save Dialog");
            UIManager.put("FileChooser.lookInLabelText", "LookIn");
            UIManager.put("FileChooser.saveInLabelText", "SaveIn");
            UIManager.put("FileChooser.upFolderToolTipText", "UpFolder");
            UIManager.put("FileChooser.homeFolderToolTipText", "HomeFolder");
            UIManager.put("FileChooser.newFolderToolTipText", "New FOlder");
            UIManager.put("FileChooser.listViewButtonToolTipText", "View");
            UIManager.put("FileChooser.detailsViewButtonToolTipText", "Details");
            UIManager.put("FileChooser.fileNameHeaderText", "Name");
            UIManager.put("FileChooser.fileSizeHeaderText", "Size");
            UIManager.put("FileChooser.fileTypeHeaderText", "Type");
            UIManager.put("FileChooser.fileDateHeaderText", "Date");
            UIManager.put("FileChooser.fileAttrHeaderText", "Attr");
            UIManager.put("FileChooser.fileNameLabelText", "Label");
            UIManager.put("FileChooser.filesOfTypeLabelText", "filesOfType");
            UIManager.put("FileChooser.openButtonText", "Open");
            UIManager.put("FileChooser.openButtonToolTipText", "Open");
            UIManager.put("FileChooser.saveButtonText", "Save");
            UIManager.put("FileChooser.saveButtonToolTipText", "Save");
            UIManager.put("FileChooser.directoryOpenButtonText", "Open Directory");
            UIManager.put("FileChooser.directoryOpenButtonToolTipText", "Open Directory");
            UIManager.put("FileChooser.cancelButtonText", "Cancel");
            UIManager.put("FileChooser.cancelButtonToolTipText", "CanMMcel");
            UIManager.put("FileChooser.newFolderErrorText", "newFolder");
            UIManager.put("FileChooser.acceptAllFileFilterText", "Accept");
            fc.updateUI();
            SwingUtilities.updateComponentTreeUI(fc);
        }
    }

    public int openFileChooser() {
        //fc.setDialogTitle("Load File);
        fc.resetChoosableFileFilters();
        int returnVal = 0;
        fc.setDialogType(JFileChooser.OPEN_DIALOG);
        returnVal = fc.showDialog(this.bfcParent, "Load File");       
        if (returnVal == JFileChooser.APPROVE_OPTION) { //Process the results.
            System.out.println("Opened");
        } else {
            System.out.println("Failed");
        }
        return returnVal;
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("FileChooser");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel jp = new JPanel(new BorderLayout());
        JButton openButton = new JButton("Open File");
        final Test test = new Test(frame, true);
        openButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                test.openFileChooser();

            }
        });
        openButton.setEnabled(true);
        jp.add(openButton, BorderLayout.AFTER_LAST_LINE);       
        frame.add(jp); //Add content to the window.        
        frame.pack();//Display the window.
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                //Turn off metal's use of bold fonts
                createAndShowGUI();
            }
        });
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • known "bugs" isn't possible to set ToolTipTex for Accept/Open File button, must be setted by using hack??? – mKorbel Aug 25 '14 at 06:08