• Post Reply Bookmark Topic Watch Topic
  • New Topic

limit text-input on a JTextField  RSS feed

 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks!

In my Swing-GUI I have an input mask made up of several JTextField components.
How is it possible to configure those fields in a way, that they only accept up to a certain number of characters?
I tried to use setSize() and javax.swing.InputVerifier but neither of these worked in the way I want it.

Maybe it is necessary to write an own EventListener listening on and counting each character inserted in the field?

Kind regards,
Andy
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a JFormattedTextField or a DocumentFilter.

Read the JTextField API and you will find a link to the Swing tutorial which contains examples for each of the above suggestions.
 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

thanks for your hint.
I think a JFormattedTextField in conjunction with a MaskFormatter will be the right choice !

Kind regards,
Andy
 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... someone else's solution to this problem:
http://www.daniweb.com/forums/thread263964.html#

But honestly, I am not content with either one of the proposed solutions.

The first one has the disadvantage, that fields are preset with a specific mask (and the mask does not even conform to a flexible regular expression style ).
The second one is far too complex for this simple requirement .

You just compare this with the simplicity of configuring that behavior in HTML.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> You just compare this with the simplicity of configuring that behavior in HTML.

and the relevance is....?
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing isn't HTML, pure and simple. I also don't see how hard it is to write (copy) a class once, then use a few extra characters to create a document and pass it to the JTextField.
 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:and the relevance is....?


... maybe this issue should be placed in a specification request for the purpose of future releases of the Java API ?
I mean, shouldn't there be an API-based convenience method, delegating that work to internal code?
Limiting an input field is probably one of the most common requirements.
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not the first one to want this. No idea why the two top ones are marked as duplicates for the JSpinner feature request though.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... someone else's solution to this problem:


And that solution is 10 years old and is NOT a good solution. That used to be the solution posted in the Swing tutorial until new Swing classes where added in JDK1.3 I believe.

I gave you a couple of the solutions that are currently used, now that JFormattedTextField and DocumentFilters have been added to the API. The tutorial has been updated to reflect both of these cases.

I hardly see how adding a couple of lines of code to create a mask os overly complex. Once you learn how to do it for simple cases you can then move on to more complex cases without completely writting custom code. Yes its a little more work than having a setMaximumCharacters(...) method, but it provides increased flexibility and people alreadly complain about API bloat.

And yes, using DocumentFilters is a little more work at first, but if you use them properly then you can create reusable code that allows you to do some interesting things as a tried to demonstrate in my Text Field Auto Tab example.
 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
..., but it provides increased flexibility and people alreadly complain about API bloat.


... talking of flexibility, I already stated that it's not flexible at all.
For my solution I used this for example:

You see it's only one line of code. But where's the flexibility here? Instead of supporting regular expressions the mask adheres to some peculiar very inflexible format.

So, I think your flexibility argument doesn't really convince me.

Besides, I already posted, that this solution using JFormattedTextField is still not the beahvior I want, and I even can't see an acceptable example in the sun tutorial.

Sorry, but that's really poor !
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got to be joking, your original requrement stated:

How is it possible to configure those fields in a way, that they only accept up to a certain number of characters?


Both solutions more than meet that requirement.

Instead of supporting regular expressions


That was NOT a requirement. I never said the mask provided "ultimate flexibility", I said it provide "increase flexibility" The fact that you can meet your original requirment in a single line of code proves this. If you want ulitmate flexibility, then create a custom DocumentFilter that does support a Regex.

Besides, I already posted, that this solution using JFormattedTextField is still not the beahvior I want,


You posted your original requirement and the solutions given do solve your problem. The fact that neight solution doesn't support a regex is irrelevant because it was not a requirement and does not make either solution less flexible.
 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
The fact that you can meet your original requirment in a single line of code proves this.

Actually it does not meet my original requirement. If you read carefully I said "up to a certain number of characters". The key is up to and not the word exactly. If I said exactly, then you would be right.
What this Formatter does behind the scenes is require exactly a certain number of characters.
For example if you reset that formatted field to an empty String "", there will be a beep in your application, this is really not nice.

And there we come to regular expression:
Rob Camick wrote:
The fact that neight solution doesn't support a regex is irrelevant because it was not a requirement and does not make either solution less flexible.

If you could use flexible regular expression syntax, you wouldn't have this problem, because then you could specify "up to" and not "exact" behavior.
See the correlation?
So, I am not joking ...
And my problem is still not solved .
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key is up to and not the word exactly.


Nowhere did you highlight the "exact" problem, which in fact is NOT a problem.

This is most definitely supported when you use a DocumentFilter.

It is also supported in the JFormattedTextField API. If fact it is specifically mentioned in the API description.

I never said the a JFormattedTextField was perfect, in fact I find it rather difficult and confusing to use. But it more than supports your simple requirment. It is still flexible by any definition. Flexible does not mean it has to support 100% of every possible requirement a programmer can dream up. So you can use it the way it is or write you own DocumentFilter is if you think regex support is that important. Otherwise get over it, not every class is written to meet your needs 100%.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this argument is becoming a little bit tedious. Can we admit that points have been made on both sides? And Andy, if you still have a question which needs to be answered, could you please summarize it? I'm a bit lost with all these excerpts and diversions.
 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.. the actual problem is:

When you post a concrete question, then you might also expect a concrete answer and not something like this:

  • look at the sun tutorial
  • look at the API
  • look at xyz ... there you can find it
  • rtfm


  • Sarcastically speaking, it's like posting "just look at google, there you will find the answer to your problem ...".
    And no shit, there was one time, where I got exactly that answer (but not on this site) ...

    The ... , if you know the answer, then just post it, giving an code example etc. !

    Sorry, but is that swing-forum a friendly place for greenhorns or is it not?
     
    Rob Spoor
    Sheriff
    Posts: 20903
    81
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rob has pointed you to DocumentFilter twice already. That should be enough for you to search on a bit. For instance, java documentfilter limit length gives me this as the first hit. If I do a search on "filter" on that page the second match is this link. That shows you 100% exactly how to implement a maximum length for text fields using a DocumentFilter.

    As for not providing full answers, we at JavaRanch like to help people find solutions themselves. We don't have FAQ entries such as ShowSomeEffort, DoYourOwnHomework and NotACodeMill for nothing. Sometimes this helping includes examples, sometimes it is just nudging you into the right direction - something Rob has done in my eyes. If you would have taken the hints and did some more research based on them you would have found a solution at least an hour ago.

    You now should know how to implement this, with the links I have just given you. You will not get it clearer than this. And no more swearing, or I will close this thread.
     
    Andy Jung
    Ranch Hand
    Posts: 150
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ... ok, I figured DocumentFilter out.
    But it's a lot of code for actually a very simple requirement I have, besides you have to subclass DocumentFilter (see the DocumentSizeFilter class) and override some methods.
    It's like wanting to paint a circle and being told to construct this object point by point because of API-flexibility ;-) ...
     
    Rob Camick
    Ranch Hand
    Posts: 2756
    12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When you post a concrete question, then you might also expect a concrete answer and not something like this:

    look at the sun tutorial
    look at the API
    look at xyz ... there you can find it
    rtfm


    You where given a concrete answer. You where given the classes to use and instructions on how to find information about using those classes. And in fact those above four answer are completely valid, and recommended answers. It is not up to us to spoon feed you the code. It is up to you to take some responsibility and do some learning on your own. The tutorial and/or API explains the classes in more detail then we have time to do.

    If you have specific question about a class after reading the tutorial, then ask a question. Don't rant because the class doesn't work exactly the way you expect it to. That is NOT our fault, we did not write the class. We can only make a recommendation based on what is supported in the JDK.

    ok, I figured DocumentFilter out.


    What do you mean you've got it figured out? All you had to do was copy the code from the tutorial which is what I originally suggested.

    It's like wanting to paint a circle and being told to construct this object point by point because of API-flexibility


    Again, quit your ranting, its not relevant to the question. And if you want a simpler solution. I've already told you that you can use the JFormattedTextField. All you have to do is set a property of the text field and you can save a value that is "upto" a given number of digits. And know I'm not going to tell you the method because of your attitude and the fact that I've already told you it is well documented in the API.

    It continues to amaze me how people new to the forums demand so much, especially since you still haven't even bothered to reply the last time I helped you.
     
    Andy Jung
    Ranch Hand
    Posts: 150
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Rob,

    don't take my "ranting" personally, it's absolutely not against you.
    I appreciate your posts a lot and I already told you that twice.
    To me this thread was already very constructive, if I wouldn't have been "ranting" so much,
    I wouldn't have been able to evaluate different solutions to this.
    However it's also my right to state, that I'm not content with all of those solutions and hints provided on that topic.
    It's just my opinion and I already know that your opinion is completely different, that's all.

    As you said I'm new to this Swing forum and I have to get to used to your approach firstly, because it is completely different to the approach I got to know in other forums on this site.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!