Is there a header with an error setting using JFileChooser.showDialog (Component, String) on ​​Mac OS X?

Search does nothing for this problem.

I am using simple code to display JFileChooser dialog with custom title and accept button:

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

      

On Windows 7, this works as expected: it displays the Save dialog box, replacing Save with MyText on the Accept button and in the dialog box.

However, on Mac OS X, only the Accept button text changes — the dialog title is empty. I am using Java SE 7 and MacOS 10.8.5.

By inserting this line between the two above:

fc.setDialogTitle("MyText");

      

The correct name will be displayed. Is this a known issue and / or can someone else reproduce this behavior?

+3


source to share


2 answers


What you are experiencing on Windows is not the expected behavior (since it is not documented), it is just a side effect of the implementation.

showDialog()

used to display a custom dialog box (for example, an Open or Save dialog box). It has an option to specify the text for the Approve button. If no title has been set using the method setDialogTitle()

, the implementation arbitrarily chooses to use the text of the claim button as the title on Windows, however this is not documented anywhere and you should not count on it.



If you want a custom header use setDialogTitle()

. If you want custom button text to approve use setApproveButtonText()

. Obviously it showDialog()

also accepts the approved button text, in which case you don't need to call setApproveButtonText()

earlier.

If you want to open a dialog, use the method showOpenDialog()

. If you want to open the Save dialog, use showSaveDialog()

. Use showDialog()

only for desired custom dialog.

+6


source


  • here all available keys for UIManager

    (some of them are not available by cyclization in UIManager

    , for all supported native OS and standard LaFs), with a notification that the success is volatille, depends on Native OS and used LaF

  • you can get parent from JFileChooser

    to useJDialog

  • add JFileChooser

    to your own JDialog

    (simple without any special settings like myDialog.add(myFileChooser);

    + myDialog.pack();

    )

  • can be played with component tree




all available keys for UIManager (some 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 GUI.
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                //Turn off metal use of bold fonts
                createAndShowGUI();
            }
        });
    }
}

      

+3


source