I want to have two textfields which will have the following validations
1) Both should be Integers
2) The value in one textfield should be a integer multiple of other

I have used two JFormattedTextFields to this. This is what i have done till now

NumberFormat updateFormat,sampleFormat;
updateFormat = NumberFormat.getNumberInstance();
sampleFormat = NumberFormat.getNumberInstance();
javax.swing.JFormattedTextField jFormattedTextField1 =new javax.swing.JFormattedTextField(updateFormat);        
javax.swing.JFormattedTextField jFormattedTextField2 = new javax.swing.JFormattedTextField(sampleFormat);

With the help of this my first part is complete
Now I am stuck with the second part, i.e. jFormattedTextField1 value should be a Integer multiple of jFormattedTextField2. I think I need to customize my NumberFormat by extending it. But How should I do that? Please help me out

Just attach listener to first field something like addInputMethodListener(InputMethodListener l) and then listen for inputMethodTextChanged(InputMethodEvent event) once that happens do your calculation for second field

I tried to used InputMethodListener but the event inputMethodTextChanged was not triggered when the text in JFormattedTextField changed. Initially I also tried use propertyChange Listener but that was triggered many times even the input was not changed so I could not control it.
Here is the code


My class implements InputMethodListener
and these were the listeners

public void inputMethodTextChanged(InputMethodEvent event) {
        //throw new UnsupportedOperationException("Not supported yet.");
System.out.println("Text Changed");


    public void caretPositionChanged(InputMethodEvent event) {
        //throw new UnsupportedOperationException("Not supported yet.");
System.out.println("Caret Changed");

Edited 5 Years Ago by gunjannigam: n/a

This article has been dead for over six months. Start a new discussion instead.