Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with JFormattedTextField!!!

 
Shawn Rieger
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've read the sun tutorials on JFormattedTextField but i just can't get it. All I want is a text field that only allows positive integer, and no limit on length. Can someone help me out and just just tell me to look at sun's tutorial.. cause thats not helping me, just making me more confused. Thanks in advance!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perhaps all you need is a textfield, setting it's document

something like this

 
Shawn Rieger
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this work for all integers but not the "-" (negative sign)?
I can type it in at runtime, but if i try to set it... String.setText("-1"); It doesn't allow it and I get an error.


[ September 09, 2005: Message edited by: Shawn Rieger ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the limiter contains
str.length() > 1

setText("-1")
is 2 characters

if you now want to allow -numbers, you'll need additional code
"-0123456789".indexOf(str) < 0
will allow
-985
9-85
985-
etc

if you intend pasting/setting the text, perhaps changing the NumberLimiter
to check Integer.parseInt(), in a try/catch block, might be a better way
passes - inserts
fails - beeps
 
Shawn Rieger
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thank you soo much. with a little modification i got it to work by looping through each char and checking to see if it matches my charater set.



I only have a few questions. what is "int offs, and AttributeSet a" for? In your original example code it doesn't proccess or use them at all. Do we need them in there?
[ September 10, 2005: Message edited by: Shawn Rieger ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> what is "int offs, and AttributeSet a" for?

int offs is the offset.
Say you had a number 123567 and you wanted to include the 4,
offs would be 3 (perhaps determined by the caret position),
to indicate where to insert the 4.


AttributeSet a, from the api docs
public interface AttributeSet
A collection of unique attributes. This is a read-only, immutable interface.
An attribute is basically a key and a value assigned to the key. The
collection may represent something like a style run, a logical style, etc.
These are generally used to describe features that will contribute to some
graphical representation such as a font. The set of possible keys is
unbounded and can be anything. Typically View implementations will respond
to attribute definitions and render something to represent the attributes.

Attributes can potentially resolve in a hierarchy. If a key doesn't resolve
locally, and a resolving parent exists, the key will be resolved through the
parent.

> Do we need them in there?

yes, but the class does all the work, you just pass them on when you
call super.insertString(...)
 
Mike Rainville
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should have a look at MaskFormatter, esp. setValidCharacters(), used with JFormattedTextField:

e.g. (from Sun's API documentation for 1.4.2

"JFormattedTextField will also support editing of strings given a mask that specifies what the legal characters are at a given character position. To create a JFormattedTextField to edit US phone numbers the following could be used:

new JFormattedTextField(new MaskFormatter("(###) ###-####"));
"
 
Brian Cole
Author
Ranch Hand
Posts: 912
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shawn Rieger:
I've read the sun tutorials on JFormattedTextField but i just can't get it. All I want is a text field that only allows positive integer, and no limit on length. Can someone help me out and just just tell me to look at sun's tutorial.. cause thats not helping me, just making me more confused.


I agree that JFormattedText field isn't documented very well. I recommend the chapter in the 2nd edition of O'Reilly's "Java Swing" book. (disclaimer: I am the author.)

Anyway, it looks like you want to be doing something like this:
<code>
NumberFormatter nf = new NumberFormatter(); // in javax.swing.text
nf.setMinimum(new Long(1));
nf.setAllowsInvalid(false);
JFormattedTextField ftf = new JFormattedTextField(nf);
</code>
 
Brian Cole
Author
Ranch Hand
Posts: 912
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brian Cole:

NumberFormatter nf = new NumberFormatter(); // in javax.swing.text
nf.setMinimum(new Long(1));
nf.setAllowsInvalid(false);
JFormattedTextField ftf = new JFormattedTextField(nf);


Or if you don't want the locale-specific delimeters
(such as commas after every 3rd digit) change the
first line to:
InternationalFormatter nf = new InternationalFormatter();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic