How to change text in JFileChooser?

12,932

Solution 1

The Swing components in Java are fully able to understand internationalization. This article explains the details and shows an example of how it can be accomplished.

Solution 2

Use UIManager

    UIManager.put("FileChooser.saveButtonText","Custom text acept");
    UIManager.put("FileChooser.cancelButtonText","custom text to cancel");
    JFileChooser fileChooser = new JFileChooser();

Solution 3

UIManager.put("FileChooser.fileNameLabelText", "FileName");
UIManager.put("FileChooser.filesOfTypeLabelText", "TypeFiles");

Solution 4

If you only need to translate JFileChooser text, I would suggest you change JFileChooser locale (by calling JFileChooser#setLocale(Locale)) instead of hacking you way into JFileChooser its internals. Indeed, all JFileChooser texts are locale dependant. As a consequence, changing locale to be the one you want will alter these texts with less efforts.

Solution 5

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.

Share:
12,932
VextoR
Author by

VextoR

Updated on June 27, 2022

Comments

  • VextoR
    VextoR almost 2 years

    In JFileChooser Java swing component I need to change all text elements (for translation):

    File name: (JLabel)

    Files of type: (JLabel)

    Cancel (JButton)

    Unfortunately, it is not any methods for that..

    Is there any way I can do it?

    Thanks!

  • Holger
    Holger over 4 years
    That only works if JFileChooser has genuine support for the particular language. Prior to JDK 11, this was limited to English, Japanese, Chinese, French, German, Italian, Korean, Portuguese (Brazilian), Spanish, and Swedish. Starting with JDK 11, Oracle decided to drop most languages (for no apparent reason). So now, this only works for English, Japanese, and Chinese. But generally, any JRE may support an arbitrary set of languages and you don’t even have a way to find out, which are currently supported…