-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
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 :
Windows XP SP2
A DESCRIPTION OF THE PROBLEM :
The example code below presents two JFormattedTextField each showing a double value with two fraction digits. The textfields difer in the way their NumberFormatter is created, but the behaviour is the same.
If the cursor is at the right edge of the textfield where no further input
should be possible, still you can type an invisible third decimal digit which
is accepted and the whole decimal part is rounded to two digits; the rounding
of course, becomes visible only if you enter ciphers >5 (or with the first
5 after a cipher >5).
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run.
Enter for example 1.976
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
1.97
or some "Field full" error
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); // Bug ID: 6575394
}
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 :
// Workaround by forum user Badajoz
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);
// Added currency or space as workaround for the NumberFormatter not to round
// the input number if the user enters ciphers >5 beyond the last decimal.
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) {
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 :
Windows XP SP2
A DESCRIPTION OF THE PROBLEM :
The example code below presents two JFormattedTextField each showing a double value with two fraction digits. The textfields difer in the way their NumberFormatter is created, but the behaviour is the same.
If the cursor is at the right edge of the textfield where no further input
should be possible, still you can type an invisible third decimal digit which
is accepted and the whole decimal part is rounded to two digits; the rounding
of course, becomes visible only if you enter ciphers >5 (or with the first
5 after a cipher >5).
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run.
Enter for example 1.976
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
1.97
or some "Field full" error
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); // Bug ID: 6575394
}
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 :
// Workaround by forum user Badajoz
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);
// Added currency or space as workaround for the NumberFormatter not to round
// the input number if the user enters ciphers >5 beyond the last decimal.
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) {
buffer.setLength(0);
buffer.append(field.getText());
}
});
}
}
- relates to
-
JDK-6794838 Caret jumps over decimal point in numeric JFormattedTextField
-
- Closed
-