JFormattedtextField and MaskFormatter

How do I tell maskformatter to check valid characters with his mask (no letter if I want a digit) and only use the valueclass constructor at the end(focuslost).

How do I enter "120" in following example and have a DivisableByThree as value in the textfield afterwards:

publicclass TryFormat{

publicstaticvoid main(String[] args){

try{

MaskFormatter formatter =new MaskFormatter("#-#-#");

formatter.setPlaceholderCharacter('0');

// lucky 000 passes, what if * is used

formatter.setValueContainsLiteralCharacters(false);

formatter.setCommitsOnValidEdit(false);

formatter.setValueClass(DivisableByThree.class);

JFormattedTextField textField =new JFormattedTextField(formatter);

JOptionPane.showMessageDialog(null, textField);

System.out.println(textField.getValue());

}catch (Exception ex){ex.printStackTrace();}

}

}

publicclass DivisableByThree{

String number;

public DivisableByThree(String number)throws ParseException{

System.out.println("DivisableByThree "+number);

if (!(Integer.parseInt(number) % 3 == 0))

thrownew ParseException("try again", 0);

this.number = number;

}

}

Thanks,

Bart

[2314 byte] By [bartvdca] at [2007-11-27 9:39:29]
# 1

import java.awt.event.FocusEvent;

import java.text.ParseException;

import javax.swing.*;

import javax.swing.text.MaskFormatter;

public class TryFormat {

public static void main( String[] args ) {

try {

MaskFormatter formatter = new MaskFormatter( "#-#-#" );

formatter.setPlaceholderCharacter( '0' );

// lucky 000 passes, what if * is used

formatter.setValueContainsLiteralCharacters( false );

formatter.setCommitsOnValidEdit( false );

formatter.setValueClass( DivisableByThree.class );

JFormattedTextField textField = new JFormattedTextField( formatter ) {

protected void processFocusEvent(FocusEvent e) {

if ( e.getID() == FocusEvent.FOCUS_GAINED ) {

((MaskFormatter)getFormatter()).setValueClass( String.class );

} else {

((MaskFormatter)getFormatter()).setValueClass( DivisableByThree.class );

}

super.processFocusEvent(e);

}

};

JOptionPane.showMessageDialog( null, textField );

System.out.println( textField.getValue() );

} catch ( Exception ex ) {

ex.printStackTrace();

}

System.exit(0);

}

static public class DivisableByThree {

String number;

public DivisableByThree( String number ) throws ParseException {

System.out.println( "DivisableByThree " + number );

if ( !( Integer.parseInt( number ) % 3 == 0 ) )

throw new ParseException( "try again", 0 );

this.number = number;

}

public String toString() { return number; }

}

}

JayDSa at 2007-7-12 23:14:58 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for this creative solution.

It works if I enter a value and push "OK". The value is set and it's a DivisableByTree.

There is just a problem when you loose focus. Fill in 123 and hit tab. The field is cleared and it's no longer possible to fill in something.

Thanks again for the reply,

Bart

bartvdca at 2007-7-12 23:14:58 > top of Java-index,Desktop,Core GUI APIs...