| META TOPICPARENT | name="SwingLabsSwingX" |
Home | Changes | Index | Search | Go <-- This creates the navigation links to : Home | Help | Index | etc. -->
SwingXLocalization <-- this automatically adds a header showing the name of this page -->
Issues
Context
Code example
/*
* Created on 21.02.2007
*
*/
package org.jdesktop.swingx.locale;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.Locale;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.action.AbstractActionExt;
/**
* Locale of JFileChooser not taken.
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4423439
*
* @author Jeanette Winzenburg, Berlin
*/
public class ToggleXLocaleBug {
private static final Locale DEFAULT_LOCALE = Locale.US;
private static final Locale ALTERNATIVE_LOCALE = Locale.GERMAN;
private Component getContent() {
JComponent comp = new JPanel(new BorderLayout());
final JXTable table = new JXTable(10, 3);
// just to see the current local
table.getColumnExt(0).setTitle(Locale.getDefault().getLanguage());
comp.add(new JScrollPane(table));
Action toggleLocale = new AbstractActionExt("toggleLocale") {
public void actionPerformed(ActionEvent e) {
Locale old = Locale.getDefault();
Locale.setDefault(old == DEFAULT_LOCALE ? ALTERNATIVE_LOCALE : DEFAULT_LOCALE);
// make sure newly created comps get the new locale by default
// Note: this does not effect components which are already
// created
JComponent.setDefaultLocale(Locale.getDefault());
table.getColumnExt(0).setTitle(Locale.getDefault().getLanguage());
}
};
// intentional: use a shared chooser to show what's required to
// make it take the new locale.
final JFileChooser chooser = new JFileChooser();
Action openFileChooser = new AbstractActionExt("open") {
public void actionPerformed(ActionEvent e) {
// we are fine if the chooser is re-created in each call
// _and_ the JComponent.defaultLocale is kept in synch with
// with Locale.getDefault()
// JFileChooser chooser = new JFileChooser();
// otherwise we have to update the chooser's locale manually
if (!Locale.getDefault().equals(chooser.getLocale())) {
chooser.setLocale(Locale.getDefault());
// need to explicitly trigger re-install to pick up the new
// locale-dependent state
chooser.updateUI();
}
chooser.showOpenDialog(null);
}
};
JComponent buttons = new JPanel();
buttons.add(new JButton(toggleLocale));
buttons.add(new JButton(openFileChooser));
comp.add(buttons, BorderLayout.SOUTH);
return comp;
}
public static void main(String[] args) {
Locale originalDefault = Locale.getDefault();
Locale def = DEFAULT_LOCALE;
Locale alt = ALTERNATIVE_LOCALE;
// make sure the default/alternative are correct for "real" us/de
if ("de".equals(originalDefault.getLanguage())) {
def = ALTERNATIVE_LOCALE;
alt = DEFAULT_LOCALE;
}
// sequence (before creating any JComponent)
// 1. set defLocal different from system def
// 2. add custom resourceBundle
// 3. reset original defLocal
// toggling lf during later lifetime doesn't find the
// filechooser's localized resources
Locale.setDefault(alt);
UIManager.getDefaults().addResourceBundle("org.jdesktop.swingx.locale.test");
Locale.setDefault(def);
final JXFrame frame = new JXFrame("Toggle Locale and use in FileChooser", true);
// // do the same sequence as above after any comp is created - okay
// Locale.setDefault(alt);
// UIManager.getDefaults().addResourceBundle("org.jdesktop.swingx.locale.test");
// Locale.setDefault(def);
ToggleXLocaleBug toggleXLocaleBug = new ToggleXLocaleBug();
frame.add(toggleXLocaleBug.getContent());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.pack();
frame.setVisible(true);
}
});
}
}
Links
<-- Your JavaDesktop? article goes here. Please try to include at least one sentence describing this topic. -->
<-- Also please try to include at least one sentence describing where each link goes. -->
<-- Please make sure some other page points to your new article so that others can find it! -->
<-- For more on how to write Javapedia articles please read the WritingArticles? page. --> |