-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
1.4.1
-
x86
-
windows_xp
Name: jk109818 Date: 07/09/2003
FULL PRODUCT VERSION :
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)
FULL OS VERSION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
With a JTextField, it is possible to add a DocumentListener to the field's
document and listen for changes. DocumentEvents are only emitted
when the user changes the text in the field, as expected.
However, using a JFormattedField, DocumentEvents are emitted every time
the field gains or loses focus (by tabbing between fields, for example),
even though the text hasn't changed.
This behaviour is unexpected. It would be nice if it were fixed to
behave like its superclass. Otherwise, it should be noted in the
documentation. Thanks for your time.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the included test program. Tab between fields.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expect there to be nothing printed to the console, as
there should be no changes emitted.
ACTUAL -
$ java TFTest
## removeUpdate: offset, length: 0, 6
## insertUpdate: offset, length: 0, 6
## text: [5.4321]
## removeUpdate: offset, length: 0, 6
## insertUpdate: offset, length: 0, 6
## text: [5.4321]
## removeUpdate: offset, length: 0, 6
## insertUpdate: offset, length: 0, 6
## text: [5.4321]
## removeUpdate: offset, length: 0, 6
## insertUpdate: offset, length: 0, 6
## text: [5.4321]
[ and so on ... ]
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.event.*;
import javax.swing.text.*;
public class TFTest extends JFrame implements DocumentListener {
public TFTest() {
super( "TFTest" );
JPanel mp = new JPanel();
mp.setLayout( new BoxLayout( mp, BoxLayout.Y_AXIS ));
for (int i=0; i < 2; i++) {
JPanel fp = new JPanel();
fp.add( new JLabel( "Field " + (i+1) + ": ", SwingConstants.RIGHT ));
JTextField tf = makeTextField();
tf.getDocument().addDocumentListener( this );
fp.add( tf );
mp.add(fp);
}
getContentPane().add( mp );
setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
}
private JTextField
makeTextField() {
JFormattedTextField ftf;
ftf = new JFormattedTextField( new DecimalFormat("0.####"));
ftf.setColumns( 10 );
try {
ftf.setValue( new Double( "5.43212345" ));
}
catch ( Exception ex ) {}
return ftf;
}
////////// DocumentListener //////////
public void
changedUpdate(DocumentEvent e) {
System.out.println("## changedUpdate");
}
public void
insertUpdate(DocumentEvent e) {
int offset = e.getOffset();
int length = e.getLength();
System.out.println("## insertUpdate: offset, length: " +
offset + ", " + length );
try {
Document doc = e.getDocument();
String txt = doc.getText( offset, length );
System.out.println("## text: [" + txt + "]" );
}
catch (BadLocationException ex ) {
System.out.println("## Bad Location: " + ex);
}
}
public void
removeUpdate(DocumentEvent e) {
int offset = e.getOffset();
int length = e.getLength();
System.out.println("## removeUpdate: offset, length: "
+ offset + ", " + length );
}
public static void
main( String args[] ) {
TFTest app = new TFTest();
app.pack();
app.setVisible( true );
}
}
---------- END SOURCE ----------
(Review ID: 187762)
======================================================================