wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JFormattedField with Custom Mask and Format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFormattedField with Custom Mask and Format" Watch "JFormattedField with Custom Mask and Format" New topic
Author

JFormattedField with Custom Mask and Format

Carlos Conti
Ranch Hand

Joined: Apr 21, 2010
Posts: 113
Hi there,

I am trying to extend a JTextField via a JFormattedField in order to obtain a textfield which accepts only numbers. This means when pressing a letter key, the control shouldn't respond to it. On the other hand when leaving the focus the entered string should format to according a specific pattern. I tried it using a JFormattedField using a MaskFormatter, but with the maskformatter I am bound to a specific text lenght, which is not the case. On the other side if only using a formatter (haven't tried it yet) I presume I won't get correct values until the control loses focus.

I need to use features of both.

A solution I have thought of is to implement my own KeyMap in the control, so actions for non valid characters return no response (or perhaps a beep!). I liked that feature from MaskFormatter. However before doing that I would appreciate any advise given to attain it, since I have seen myself many times developing things which could have saved, if had had deeper understanding of the component suite.

Thanks in advance and regards,

Carlos.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Use a custom DocumentFilter sub class on a regular text field's (Abstract)Document. Override insertString and replace:
You can also modify the string / text variable to strip out any non-digit before calling super.insertString / super.replace.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Carlos Conti
Ranch Hand

Joined: Apr 21, 2010
Posts: 113
Hi rob, thanks for the answer. Looks cool. Tried overriding the insertString method in the Document instanceof the textField and works smoothly, however I would like to implement it via the Documentfilter, but not getting it to work (not attaching the DocumentFilter somehow and the overriden method not reached), am i missing something very stupid? the following code should do isnt'it?

I am implementing it inside the DefaultCellEditor (CuentaTableCellEditor) class, which holds a reference to the textField involved in managing everything.

Thanks again and regards,

Carlos.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

I've modified your code a bit into an SSCCE, and it appears it's never calling insertString, only replace. So override that one too.

Also, don't catch the BadLocationException in the call to super.insertString or super.replace - just throw it on. It means some code is trying to do something bad, and you don't want to silently ignore that. You usually shouldn't see that exception being thrown anyway.
Carlos Conti
Ranch Hand

Joined: Apr 21, 2010
Posts: 113
Yes,

I had to override the method as you say, I saw it accidentally on a related post. Definetely this approach is the simplest way to attain it, which at the time doesn't interfer with other gui aspects.

Thanks for pointing me on this direction Rob. Had the chance to have a closer look to text controls and their inner working.

Regards,

Carlos.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JFormattedField with Custom Mask and Format
 
Similar Threads
add Format after component creation
Avoid user entering more than 2 decimal figures in textfield
set the max length of a JTextField
setting limited number of characters for a text field
Problems in Focus