"Il y a peu de choses qui me soient impossibles..."
Stevens Miller wrote:
No idea why you are having the two-character problem you describe, but I'd suggest you combine your filters into one class and try to debug that. Maybe the problem will seem easy then, or maybe it will just go away by itself.
There are three kinds of actuaries: those who can count, and those who can't.
So I though I would merge the two threads to bring your answer to everybody's attention. But I have done something wrong and the answer isn't herePiet Souris wrote:I gave a possible solution in your other thread. . . .
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string
Campbell Ritchie wrote:That looks incredibly complicated.
You can use a regular expression instead if you wish.
Campbell Ritchie wrote:That looks incredibly complicated.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:Go through the Java™ Tutorials which has a nice section about regexes.
Winston Gutkowski wrote:
Campbell Ritchie wrote:That looks incredibly complicated.
Actually, the whole darn Text API looks God-awful. Why cant I just say:
JTextField text = new JTextField();
text.setMaxInputLength(8);
and have Swing bleedin' well do it for me? Surely to God it can't be the first time somebody's asked for this...
But nooo ... I have to plough through Documents and EditorKits and ActionEvents ... I dunno.
I want a text field that's 8 characters long, that's all - you'd think I was asking for the friggin' moon...
Gawd I'm glad I don't have to write GUI code.
Winston
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:hmm, well it is actually not that difficult...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Piet Souris wrote:hmm, well it is actually not that difficult...
I suspect you and me have very different ideas of "difficult".
There are three kinds of actuaries: those who can count, and those who can't.
There are three kinds of actuaries: those who can count, and those who can't.
Thank youPiet Souris wrote:Errare Humanum Est! . . .
Piet Souris wrote:
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:You shouldn't set the filter when a key is pressed. Instead, you should set the filter at the start of your code. When you type it tries to insert text in the document. But because the document has a filter specified, the filter is triggered.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
rodolfo tuble wrote:I am trying to learn on how I can limit the length of my textfield in java to 7 digits, and also limit it to only accept numeric characters. (...)
There are three kinds of actuaries: those who can count, and those who can't.
Rob Spoor wrote:Basically where you now attach the listener, you instead attach the filter. AbstractDocument will take care that it's called.
Piet Souris wrote:
The use case is simply:
and possibly later,
Since NumericField extends JTextField, you can add all
kinds of Listeners to it, just like you could do to any other
JTextField.
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:hi rodolfo,
where you define your 'amount' JTextfield, can you do this:
There is no need to add any keylisteners (or Document Listeners)
to this field. Just maybe an ActionListener to catch an 'enter' from
the keyboard.
Can you tell me if that is what you need?
rodolfo tuble wrote:
I am using Netbeans for my UI would the declaration of the amount JTextfield be found inside the initComponents?
Also, I was hoping to implement the limit during user input and not on enter, is this possible?
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:
Also, I was hoping to implement the limit during user input and not on enter, is this possible?
Why would you do a thing like that?
But is is possible. If you look at the code you see that there is a variable
that sets the maximum number of digits. Right after where a string is added,
just set that variable to whatever value you like.
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:It is up to you to add all kinds of Listeners or not.
The only thing I did was to provide a class ('NumericField') that
is a JTextField, accepts only digits up to a maximum number that
you can set at contruction time. Nothing more, nothing less.
I thought that that was what you were asking.
Go to my reply dd 2 december, where I give a working example.
Now, copy that example into NetBeans, run it and tell me if
that suits your needs.
If not, please explain why you need something different.
rodolfo tuble wrote:
So I was only able to run the first one and it is what I need though I am still having trouble implementing it in my code, I tried this:
Can you help me figure out how do I add this in my code, also if how would I change it to also trap inputs from the password field. Thank You
(...)
how would I change it to also trap inputs from the password field
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:
The extended version of 'NumericField' does not give me any error. Can you show me
how you tested it? A small program like I delivered shoud do.
(...)
how would I change it to also trap inputs from the password field
What exactly do you mean by "trap the input"? If you want to get informed
when a user presses <return>, you must add an ActionListener to your textfield.
Since NumericField is a JTextField, you can add an ActionListener in the normal way.
By the way: if you look at my code, you see that I check the string-to-be-input
againt a length of 1, and also that it is a digit. By changing this, you can set it
to accept any character you like. You can even make it into a JPassWordField by
inserting a '*' for instance, instead of the typed in character.
Well, enough for you to try out.
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:How did you layout this card1 panel?
But adding 'nf2' should at least make it visible!
If you are using NetBeans for the layout of card1,
then it is in all likelihood using a GroupLayoutManager,
and adding a component like a JTextField to such a
manager by hand is not an easy task; it is certainly not as easy
as 'card1.add(nf2)'.
The problem with using NetBeans for your layout is when
you need to add a dedicated component that is NOT in the
component palette! Although you can drag a JTextField into
your panel, you cannot drag a 'NumericField' into that same
panel.
I explained a way out for this, but before going into that
with some more details, can you explain first how you layout
your cardXX panels?
And: do you at least see 'nf1' appearing in 'card17'?
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:Well, your application is very ambitious, that is for sure!
What you do is the following:
1) go to your 'card17' panel in NetBeans, using the GUI builder
2) drag a JPanel onto 'card17', at about the place where our 'nf1' should appear.
3) give this panel a clear name, and give it a distinctive backgroundcolor. Suppose we name
this JPanel 'PanelForNumericFieldnf1'.
4) now, if you press the 'Source' button, in the button bar right above the program-panel,
('Source' is the left most one), you will see the code that NetBeans generated.
It looks horrible, but let that not scare you.
5) locate the line with the code 'initComponents()';
6) type right below this line, this code: initMyOwnVariables();
7) create a method with this name. In this method you can initialize all your own
variables. It is here that we will create 'nf1'
8) So, type in:
And finally, run your program. If you click on your tab 'card17' you should now see
the NumericField.
If all is okay, you can set the background of that panel to transparant.
There are three kinds of actuaries: those who can count, and those who can't.
Consider Paul's rocket mass heater. |