Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 630
ags Avatar asked Dec 19 '22 11:12

ags


2 Answers

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.

like image 179
icza Avatar answered Jan 04 '23 23:01

icza


  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();
            }
        });
    }
}
like image 30
mKorbel Avatar answered Jan 04 '23 22:01

mKorbel