Is there a bug setting title using JFileChooser.showDialog(Component, String) in Mac OS X?

大城市里の小女人 提交于 2019-12-05 12:25:24

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.

mKorbel
  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();
            }
        });
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!