-
Bug
-
Resolution: Won't Fix
-
P3
-
6
-
x86
-
windows_xp
FULL PRODUCT VERSION :
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Windoes XP SP2
A DESCRIPTION OF THE PROBLEM :
If the value starts with "0." and the cursor is before the decimal point, the
cursor is incorrectly moved to the right of the decimal point after the first
entered cypher.
If the value does NOT start with "0.", for example "1." everything is ok.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run.
Move the caret to the right of the initial zero and enter "123".
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
123.00
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.text.NumberFormatter;
public class Decimal extends JFrame {
JFormattedTextField ftf1 = new JFormattedTextField();
JFormattedTextField ftf2 = new JFormattedTextField();
public Decimal() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(200, 150);
try {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumIntegerDigits(1);
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
NumberFormatter formatter = new NumberFormatter(nf);
formatter.setAllowsInvalid(false);
ftf1 = new JFormattedTextField(formatter);
ftf1.setHorizontalAlignment(SwingConstants.RIGHT);
ftf1.setValue(new Double(0.));
// Doesn't work, even when put in SwingUtilities.invokeLater(...).
// ftf1.setCaretPosition(1);
}
catch (Exception ex) {
System.out.println(ex);
}
try {
NumberFormatter formatter = new NumberFormatter(
new DecimalFormat("#,##0.00"));
formatter.setAllowsInvalid(false);
ftf2 = new JFormattedTextField(formatter);
ftf2.setHorizontalAlignment(SwingConstants.RIGHT);
ftf2.setValue(new Double(0.));
}
catch (Exception ex) {
System.out.println(ex);
}
Container cp= getContentPane();
cp.setLayout(null);
ftf1.setBounds(20, 11, 139, 20);
ftf2.setBounds(20, 41, 139, 20);
cp.add(ftf1);
cp.add(ftf2);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Decimal();
}
});
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.NumberFormatter;
public class Decimal1 extends JFrame {
DecimalFormatSymbols dfs= new DecimalFormatSymbols();
JFormattedTextField ftf = new JFormattedTextField();
public Decimal1() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(200, 150);
NumberFormatter formatter = new NumberFormatter(
new DecimalFormat("#,##0.00"));
formatter.setAllowsInvalid(false);
ftf = new JFormattedTextField(formatter);
ftf.setHorizontalAlignment(SwingConstants.RIGHT);
ftf.setValue(new Double(0.));
formatText(ftf);// Workaround
Container cp= getContentPane();
cp.setLayout(null);
ftf.setBounds(20, 11, 139, 20);
cp.add(ftf);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Decimal1();
}
});
}
private void formatText(final JFormattedTextField field) {
final StringBuffer buffer = new StringBuffer();
final String initial0= "0"+dfs.getDecimalSeparator();
field.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
// Set the caret to original position if the value starts with "0." and the
// cursor is before the decimal point.
if (buffer.toString().startsWith(initial0) &&
!buffer.toString().equals(field.getText()) &&
field.getCaretPosition() == 2) {
field.setCaretPosition(1);
buffer.setLength(0);
buffer.append(field.getText());
}
}
});
field.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
if (field.getText().startsWith(initial0)) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
field.setCaretPosition(1);
}
});
}
}
});
field.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if (e.getKeyChar()==dfs.getDecimalSeparator()) {
// Remember trailing blank.
field.setCaretPosition(field.getText().length()-3);
}
buffer.setLength(0);
buffer.append(field.getText());
}
});
}
}
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Windoes XP SP2
A DESCRIPTION OF THE PROBLEM :
If the value starts with "0." and the cursor is before the decimal point, the
cursor is incorrectly moved to the right of the decimal point after the first
entered cypher.
If the value does NOT start with "0.", for example "1." everything is ok.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run.
Move the caret to the right of the initial zero and enter "123".
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
123.00
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.text.NumberFormatter;
public class Decimal extends JFrame {
JFormattedTextField ftf1 = new JFormattedTextField();
JFormattedTextField ftf2 = new JFormattedTextField();
public Decimal() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(200, 150);
try {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumIntegerDigits(1);
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
NumberFormatter formatter = new NumberFormatter(nf);
formatter.setAllowsInvalid(false);
ftf1 = new JFormattedTextField(formatter);
ftf1.setHorizontalAlignment(SwingConstants.RIGHT);
ftf1.setValue(new Double(0.));
// Doesn't work, even when put in SwingUtilities.invokeLater(...).
// ftf1.setCaretPosition(1);
}
catch (Exception ex) {
System.out.println(ex);
}
try {
NumberFormatter formatter = new NumberFormatter(
new DecimalFormat("#,##0.00"));
formatter.setAllowsInvalid(false);
ftf2 = new JFormattedTextField(formatter);
ftf2.setHorizontalAlignment(SwingConstants.RIGHT);
ftf2.setValue(new Double(0.));
}
catch (Exception ex) {
System.out.println(ex);
}
Container cp= getContentPane();
cp.setLayout(null);
ftf1.setBounds(20, 11, 139, 20);
ftf2.setBounds(20, 41, 139, 20);
cp.add(ftf1);
cp.add(ftf2);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Decimal();
}
});
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.NumberFormatter;
public class Decimal1 extends JFrame {
DecimalFormatSymbols dfs= new DecimalFormatSymbols();
JFormattedTextField ftf = new JFormattedTextField();
public Decimal1() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(200, 150);
NumberFormatter formatter = new NumberFormatter(
new DecimalFormat("#,##0.00"));
formatter.setAllowsInvalid(false);
ftf = new JFormattedTextField(formatter);
ftf.setHorizontalAlignment(SwingConstants.RIGHT);
ftf.setValue(new Double(0.));
formatText(ftf);// Workaround
Container cp= getContentPane();
cp.setLayout(null);
ftf.setBounds(20, 11, 139, 20);
cp.add(ftf);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Decimal1();
}
});
}
private void formatText(final JFormattedTextField field) {
final StringBuffer buffer = new StringBuffer();
final String initial0= "0"+dfs.getDecimalSeparator();
field.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
// Set the caret to original position if the value starts with "0." and the
// cursor is before the decimal point.
if (buffer.toString().startsWith(initial0) &&
!buffer.toString().equals(field.getText()) &&
field.getCaretPosition() == 2) {
field.setCaretPosition(1);
buffer.setLength(0);
buffer.append(field.getText());
}
}
});
field.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
if (field.getText().startsWith(initial0)) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
field.setCaretPosition(1);
}
});
}
}
});
field.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if (e.getKeyChar()==dfs.getDecimalSeparator()) {
// Remember trailing blank.
field.setCaretPosition(field.getText().length()-3);
}
buffer.setLength(0);
buffer.append(field.getText());
}
});
}
}
- relates to
-
JDK-6794837 Unwanted rounding of numeric values in JFormattedTextField
-
- Closed
-