1

There are several components in Java that have predefined look and strings of text that are automatically printed on them. Examples is JFileChooser.

Also, there is a JDialog (or JOptionPane) that pops up when you try to do illegale rename in JFileChooser...

In what *.java file(s) can string keys that represent that keys and where do they get their values?

I'm talking about Nimbus L&F... I couldn't locate them in Nimbus nor Synth (which doesn't necessary mean they're not there)... I did found JFileChooser Strings in BasicFileChooser.

Bottom line: I'm translating my program and I don't want any surprises, so I'd like to know which components have predefined strings and where to find them, that JDialog from above especially...

EDIT: I have found BasicFileChooserUI, and this is one of the methods:

protected void installStrings(JFileChooser fc) {

    Locale l = fc.getLocale();
    newFolderErrorText = UIManager.getString("FileChooser.newFolderErrorText",l);
    newFolderErrorSeparator = UIManager.getString("FileChooser.newFolderErrorSeparator",l);

    newFolderParentDoesntExistTitleText = UIManager.getString("FileChooser.newFolderParentDoesntExistTitleText", l);
    newFolderParentDoesntExistText = UIManager.getString("FileChooser.newFolderParentDoesntExistText", l);

    fileDescriptionText = UIManager.getString("FileChooser.fileDescriptionText",l);
    directoryDescriptionText = UIManager.getString("FileChooser.directoryDescriptionText",l);

    saveButtonText   = UIManager.getString("FileChooser.saveButtonText",l);
    openButtonText   = UIManager.getString("FileChooser.openButtonText",l);
    saveDialogTitleText = UIManager.getString("FileChooser.saveDialogTitleText",l);
    openDialogTitleText = UIManager.getString("FileChooser.openDialogTitleText",l);
    cancelButtonText = UIManager.getString("FileChooser.cancelButtonText",l);
    updateButtonText = UIManager.getString("FileChooser.updateButtonText",l);
    helpButtonText   = UIManager.getString("FileChooser.helpButtonText",l);
    directoryOpenButtonText = UIManager.getString("FileChooser.directoryOpenButtonText",l);

    saveButtonMnemonic   = getMnemonic("FileChooser.saveButtonMnemonic", l);
    openButtonMnemonic   = getMnemonic("FileChooser.openButtonMnemonic", l);
    cancelButtonMnemonic = getMnemonic("FileChooser.cancelButtonMnemonic", l);
    updateButtonMnemonic = getMnemonic("FileChooser.updateButtonMnemonic", l);
    helpButtonMnemonic   = getMnemonic("FileChooser.helpButtonMnemonic", l);
    directoryOpenButtonMnemonic = getMnemonic("FileChooser.directoryOpenButtonMnemonic", l);

    saveButtonToolTipText   = UIManager.getString("FileChooser.saveButtonToolTipText",l);
    openButtonToolTipText   = UIManager.getString("FileChooser.openButtonToolTipText",l);
    cancelButtonToolTipText = UIManager.getString("FileChooser.cancelButtonToolTipText",l);
    updateButtonToolTipText = UIManager.getString("FileChooser.updateButtonToolTipText",l);
    helpButtonToolTipText   = UIManager.getString("FileChooser.helpButtonToolTipText",l);
    directoryOpenButtonToolTipText = UIManager.getString("FileChooser.directoryOpenButtonToolTipText",l);
}

I want to know from where is the getString("FileChooser.updateButtonText",l) method pulling out strings... I tried looking for it, but I had no luck... Also, I know there are some strings in JFileChooser that are not defined in BasicFileChooserUI.java...

Ivan Karlovic
  • 221
  • 5
  • 14

3 Answers3

2

Many such user interface elements are already localized for supported languages, as shown in JDK 6 and JRE 6 Supported Locales: User Interface Translation.

Addenda: See also Internationalization: Understanding Locale in the Java Platform. The manner in which UIManager.getLookAndFeelDefaults() obtains the L&F defaults is not specified; changing the source data is not supported. The (non-localized) names of the properties found in the returned Map may be used to override the defaults. As discussed in How to Write a Custom Look and Feel, the source text is stored in a properties file for each L&F and each supported locale. QuaQua is an example. On my platform, for example, the English strings for com.apple.laf.AquaLookAndFeel are in

$JAVA_HOME/Resources/English.lproj/aqua.properties

which warns:

# When this file is read in, the strings are put into the 
# defaults table.  This is an implementation detail of the current
# workings of Swing.  DO NOT DEPEND ON THIS.  This may change in
# future versions of Swing as we improve localization support.

See also How can I add localization to JFileChooser for a locale that is not supported by default?

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Yeah, Croatian is spoken by 4.5 milion people, I think I'll have to do it manually... I need location where are ALL the strings initialized... BasicFileChooser contains some, but not all... It just need to find the file taht contains "FileChooser.diferentstringtext" strings kinda type, becuse thats what i send to UIManager... – Ivan Karlovic Sep 20 '12 at 20:36
  • not sure maybe he needed the exaplanations about Swing and its implemented or inherirs menthod from AWT, these Object (coumpound in most cases) are based on properties that came from Native OS and inherits methods from AWT classes – mKorbel Sep 20 '12 at 20:36
  • @Ivan Karlovic why do you need to translaslate the labels and narratives, Locale doesn't works, or you to use some **nix or solaris OS – mKorbel Sep 20 '12 at 20:38
  • I want to make multi-language application... I want user to be able to choose... It also has to be platform independant... – Ivan Karlovic Sep 20 '12 at 20:39
  • basically JFileChooser to accepting Locale or Localizations that sets in Native OS, there are a few ways for a few JComponents, no issue to rename choices for JOptionsPane, JFileChooser is compound JComponents, there no issue to derive that to the elements and I missed another JComponents that needed to translate – mKorbel Sep 20 '12 at 20:43
  • I would defer to @mKorbel's deeper insight into this. You might edit your question to indicate which unsupported locales you plan to support. – trashgod Sep 20 '12 at 20:43
  • I intend to translate it into Croatian... It's not supported... There are a lot of files, and I'm not to familiar with whole java structure, so I'm asking someone who is to tell me in which files to seek for that strings I'm interested in... I'm guessing that it's in the sam level folder conatining all components... – Ivan Karlovic Sep 20 '12 at 20:47
  • @Ivan Karlovic notice, nice talk but in this form your question is too hard answerable, if you have got issue, post a question about the concrete JComponent, no idea how deeper you want to go, btw I think those labels are the same for all L&F, maybe I'm wrong, never ever compared – mKorbel Sep 20 '12 at 20:47
  • basically you have look at BasicXxxUI, if there are differencies, for example in MetalXxxUI, then is probably the mistake and report that to the BugParade (still we talking about labels and naratives for compound JComponents) – mKorbel Sep 20 '12 at 20:49
  • `here are a lot of files, and I'm not to familiar with whole java structure,` not, not not we can talking about three four JComponents, compaund JComponents that are based on properties that came from Native OS, rest is managable from UImanager, dot ... – mKorbel Sep 20 '12 at 20:52
  • wait a sec I have to check & compare Keys in UIManager (true is have to smoke) – mKorbel Sep 20 '12 at 20:55
  • P.S. let's try finging them for JFileChooser, I'm guessing that the ones for others will be in the same location... – Ivan Karlovic Sep 20 '12 at 20:59
  • those methods are protected in [BasicFileChooserUI](http://docs.oracle.com/javase/7/docs/api/javax/swing/plaf/basic/BasicFileChooserUI.html), but never hands up – mKorbel Sep 20 '12 at 21:10
  • @IvanKarlovic: To my knowledge, altering the source properties is not supported; more above. Of course, I'm happy to be proven wrong. – trashgod Sep 20 '12 at 21:12
  • I know where those method are, I want to know where are they ultimatly leeching their arguments from, the first one especially: `"FileChooser.blablablatext"` – Ivan Karlovic Sep 20 '12 at 21:25
2

which one you want to change, but I don't know answer now

enter image description here

DYM???

look in:

file name:

files of type:

import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;

class ChooserFilterTest {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                String[] properties = {"os.name", "java.version", "java.vm.version", "java.vendor"};
                for (String property : properties) {
                    System.out.println(property + ": " + System.getProperty(property));
                }
                JFileChooser jfc = new JFileChooser();
                jfc.showOpenDialog(null);
                jfc.addChoosableFileFilter(new FileFilter() {

                    @Override
                    public boolean accept(File f) {
                        return f.isDirectory() || f.getName().toLowerCase().endsWith(".obj");
                    }

                    @Override
                    public String getDescription() {
                        return "Wavefront OBJ (*.obj)";
                    }

                    @Override
                    public String toString() {
                        return getDescription();
                    }
                });
                int result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (Metal): " + (result == JOptionPane.YES_OPTION));
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(jfc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                jfc.showOpenDialog(null);
                result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (System): " + (result == JOptionPane.YES_OPTION));
                result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (Metal): " + (result == JOptionPane.YES_OPTION));
                try {
                    for (UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            SwingUtilities.updateComponentTreeUI(jfc);
                            break;
                        }
                    }
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                }
                jfc.showOpenDialog(null);
                result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                System.out.println("Displayed description (System): " + (result == JOptionPane.YES_OPTION));
            }
        };
        SwingUtilities.invokeLater(r);
    }

    private ChooserFilterTest() {
    }
}

Do you want this one

enter image description here

from code

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.*;
import javax.swing.plaf.metal.MetalButtonUI;

public class CrazyFileChooser {

    public static void main(String[] args) {
        try {
            for (UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        }


        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new CrazyFileChooser().makeUI();
            }
        });
    }

    public void makeUI() {
        JFileChooser chooser = new JFileChooser();
        for (AbstractButton button : SwingUtils.getDescendantsOfType(AbstractButton.class, chooser)) {
            button.setUI(new XORButtonUI());
            button.setForeground(Color.GREEN);
        }
        for (JList list : SwingUtils.getDescendantsOfType(JList.class, chooser)) {
            list.setBackground(Color.PINK);
        }
        JTextField jTextField = SwingUtils.getDescendantOfType(JTextField.class, chooser, "Text", "");
        jTextField.setEditable(false);
        for (JLabel label : SwingUtils.getDescendantsOfType(JLabel.class, chooser)) {
            label.setFont(new Font("Dialog", Font.ITALIC, 18));
            label.setForeground(Color.RED);
        }
        chooser.showOpenDialog(null);
    }
}

class XORButtonUI extends MetalButtonUI {

    @Override
    public void paint(Graphics g, JComponent c) {
        g.setXORMode(Color.YELLOW);
        super.paint(g, c);
    }
}

based on code Swing Utils, by Darryl Burke, one of top Swing gurus (once told us, to pay me for the programming, is how to pay a small child for licking ice cream)

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • @Ivan Karlovic rest is up to you, don't forget to publish Croatian Locale to the next Java updates (-: Java7 :-) – mKorbel Sep 20 '12 at 21:40
1

These keys are provided by Swing PLAF resource bundles, and you can find them in the JDK sources. See e.g.:

String values for languages other than English are provided by adjacent bundle files.

And you can add one more bundle to any of these families just by creating one more file for desired human language and placing it anywhere on your classpath. Bundles in .java and .properties format work equally well, though .java format may be slightly more Unicode-friendly...

It may be good to keep in mind though that direct adding of content to com.sun package may violate the Java license. So to be on the safe side, it may be wise to move your extra resources to a package of your own and register it with UIManager like this:

UIManager.getDefaults().addResourceBundle("mypackage.swing.plaf.basic.resources.basic");

And as for Nimbus, I did not find any special resources for it, so going with "basic" may do the job...

Sergey Ushakov
  • 2,425
  • 1
  • 24
  • 15