'Percent Sign within JFormattedTextField appears spaced differently between Linux and Windows
On Linux, the percent sign appears separated from the corresponding value within a JFormattedTextField. Like this: 123,0000000000 %.
On the other hand, on Windows, it's displayed attached to the value: 123,0000000000% (no space in between). Hence, double-clicking to only edit the value becomes a pain on Windows.
The locale is German.
Can I force Windows to put the percent sign separately, like Linux does?
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.*;
import javax.swing.text.*;
import java.beans.PropertyChangeListener;
public class PercentSignSpacing extends JPanel implements PropertyChangeListener {
private JFormattedTextField textField;
private NumberFormat pFormat;
public PercentSignSpacing() {
pFormat = NumberFormat.getPercentInstance(Locale.GERMANY);
pFormat.setMinimumFractionDigits(10);
textField = new JFormattedTextField(new DefaultFormatterFactory(new NumberFormatter(pFormat),
new NumberFormatter(pFormat), new NumberFormatter(pFormat)));
textField.setValue(123.0 / 100);
textField.setColumns(20);
textField.addPropertyChangeListener("value", this);
add(textField);
}
public void propertyChange(PropertyChangeEvent e) {
textField.setValue(((Number) textField.getValue()).doubleValue());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Percent");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0, 1));
frame.add(new PercentSignSpacing());
frame.add(new PercentSignSpacing());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
