Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JFormattedTextField does not update its value  RSS feed

 
Dimitar Gospodinov
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have single JFormattedText field. I want to be able to enter integer numbers with maximum of 10 digits.
I use the code shown below.

If I enter 12345678901111 and tab out of the text field, the field changes to 5678901111, but its property Value remains 12345678901111.

Anybody ever experienced this behavior?

It looks like the problem is in the Format subclasses. There are several parse... methods and they ignore min/max number of digits attributes; their counterpart - the format... methods take into account min/man number of digits and this is why the text field is set correctly. This is just my explanation of the problem.

Am I doing something incorrectly? Or is this a bug with the Format subclasses?

Thanks,

Dimitar



import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.text.*;
import java.beans.*;

public class Test1 {
public static void main(String[] args) {
JFrame f = new JFrame("Test");
f.setSize(800, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new BoxLayout(f.getContentPane(),BoxLayout.Y_AXIS));

final NumberFormat df1 = DecimalFormat.getIntegerInstance();
df1.setGroupingUsed(false);
df1.setParseIntegerOnly(true);
df1.setMaximumIntegerDigits(10);

final NumberFormatter nf = new NumberFormatter(df1);
nf.setValueClass(Long.class);

final JFormattedTextField ftf = new MyFTF(nf);
ftf.setColumns(20);

ftf.addPropertyChangeListener("value",
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
System.out.println("Property changed to " + ftf.getValue() +
" text shows " + ftf.getText());
}
}
);

JPanel p = new JPanel();
p.add(ftf);
JButton b = new JButton("Value");
p.add(b);
f.getContentPane().add(p);
f.show();
}
}

class MyFTF extends JFormattedTextField {

public MyFTF(NumberFormatter n) {
super(n);
}

protected void processFocusEvent(FocusEvent e) {
super.processFocusEvent(e);
if (e.isTemporary()) return;
if (e.getID() == FocusEvent.FOCUS_LOST) {
System.out.println("In processFocusEvent: value is " +
getValue() + " *** text value is: " + getText()
);
}
}
}
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it required to use the NumberFormat class in your application? As far as I see in your code and your requirement, the solution should be easier to get than your current implementation...

You can just use simple TextField, get value and substring the first 10 integers... If any implmentation is allowed in your application, it's better to use simple technique and get the result...

Hope it helps...
 
Dimitar Gospodinov
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanted to use JFormattedTextField.
Yes, I can write custom code to restrict user input, but the point here is to use something ready and not write my own implementation.

Dimitar
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!