Location of String keys in L&F

一世执手 提交于 2019-12-17 17:22:55

问题


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...


回答1:


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

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

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)




回答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?




回答3:


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

  • http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/com/sun/swing/internal/plaf/basic/resources/basic.java
  • http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/com/sun/java/swing/plaf/windows/resources/windows.java
  • http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/com/sun/java/swing/plaf/gtk/resources/gtk.java
  • ...

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...



来源:https://stackoverflow.com/questions/12519951/location-of-string-keys-in-lf

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