• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

implementing 2 document filter at the same time on keypressed

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:I created a new JFrame application i NetBeans, dragged a JPanel into the frame
and called it 'PanelForNumericFieldnf1'.

Therewas one thing I had to do as well: I had to set the Layoutmanager of
this panel to 'FlowLayout'. I missed that in my earlier step-by-step description.
Right click on that panel, and in the pop-up menu choose 'Set Layout' and go
for 'FlowLayout'.

(for technical reasons I had to change the name to 'NumericField1').

Run this code, and you will see this NumericField appearing in the panel.
If you replace this panel with your 'card17', you should get the same
outcome.

One remark:

in your version of ' initMyOwnVariables()' I see:

'nf1' is now a local variable, not known outside of this method.

If you look at my code, you'll see I made that component a member variable.
Th advantage is is that I have this variable available everywhere in the code,
but whether that is necessary, I don't know. But for now it might be safest to do so.



I tried the code and I have to ask how did you manage to add a textfield on a jpanel? I just can't seem to understand how it was done. Also, it worked on my part but the problem is that I already have an existing textfield in my pane on which I want to use your numericfield on and not add a new textfield. Is this possible? Also why is flowlayout necessary? as I am using null layout ATM for my UI placement and using flowlayout sadly destroys my arrangement. Thank you
 
Saloon Keeper
Posts: 5583
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi rodolfo,

hmmm... it iseems that sofar I mispredicted nearly everything
about what I thought you had coded.

Let me put my assumptions about your code:

1) you have a frame, and in that frame you have a ContentPane
that uses a CardLayout.

2) there are (at least) 17 "cards" (JPanels) contained in this ContentPane.

3) at least card1 and card17 were created with the help of NetBeans GUI Builder.
For both panels you used a Null Layout.

4) card1 and card17 contain a JTextField, that should accept onlt digits up to a
maximum of 7

Am I right?

Let's assume it for now. The question is: how to replace these two JTextFields
with two NumericFields?

Well, leave everything as it is for the time being. In the method 'initMyOwnVariables()'
we will do this replacement. So here goes:

I assume that the JTextField in card1 is called 'jTextField1'.

You see how I add to and remove components from a JPanel.
My previous steps-plan was based on the idea that you used 'GroupLayout'
for every cardXX.
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi rodolfo,

hmmm... it iseems that sofar I mispredicted nearly everything
about what I thought you had coded.

Let me put my assumptions about your code:

1) you have a frame, and in that frame you have a ContentPane
that uses a CardLayout.

2) there are (at least) 17 "cards" (JPanels) contained in this ContentPane.

3) at least card1 and card17 were created with the help of NetBeans GUI Builder.
For both panels you used a Null Layout.

4) card1 and card17 contain a JTextField, that should accept onlt digits up to a
maximum of 7

Am I right?

Let's assume it for now. The question is: how to replace these two JTextFields
with two NumericFields?

Well, leave everything as it is for the time being. In the method 'initMyOwnVariables()'
we will do this replacement. So here goes:

I assume that the JTextField in card1 is called 'jTextField1'.

You see how I add to and remove components from a JPanel.
My previous steps-plan was based on the idea that you used 'GroupLayout'
for every cardXX.



That's exactly it! I'll give this a try, and as always thank you. Also can you enlighten me on how you manage to add a jtextfield on that panel?



I tried the code above and no textfield appeared in the panel, I should also note that I am using a jlabel as a background image.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rodolfo tuble wrote:I tried the code above and no textfield appeared in the panel, I should also note that I am using a jlabel as a background image.


Blimey, this thread has mushroonmed.

Most of the GUI stuff is much too complicated for me, but I did wonder what
  if ( !"0123456789".contains(str) )...
was supposed to do.

Because if it's supposed to be a "numeric" check, it's wrong. On the other hand, if the string can only contain numeric digits, then
  if ( str.replaceAll("[0-9]+", "").length() == 0 ) ... // then 'str' is numeric
should work; or conversely:
  if ( str.replaceAll("[^0-9]+", "").length() == str.length() ) ...
which might be a bit faster.

Oddly enough, PL/1 had a lovely method called 'verify()', which I've never seen anywhere else, which basically looked like this:And
  if (verify(someString, "0123456789") < 0) ...
tests if a string is numeric.

HIH

Winston
 
Piet Souris
Saloon Keeper
Posts: 5583
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:(...)
Because if it's supposed to be a "numeric" check, it's wrong.


It is indeed supposed to be a numeric check, and no, it is not wrong.
Please have a close look at the code I supplied.

What about a simple 'string.match("...")' for a check if string contains only digits?

@rodolfo
In this thread, and in your other thread about centering lines,
you used pretty advanced GUI stuff like document filters and printJobs.
So, if you are asking me how to add a JTextField to a JPanel, while I
have given quite some examples now of how to get a NumericField
into your panels, that I cannot help to think you mean something
different with this question.

So I have a request: can you zip your project (i.e. the folder that contains
all of your code, including possibly some other resources), put in on
Git, DropBox. Google Drive or similar, so that I can have a look at
what you have so far?
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:

Winston Gutkowski wrote:(...)
Because if it's supposed to be a "numeric" check, it's wrong.


It is indeed supposed to be a numeric check, and no, it is not wrong.
Please have a close look at the code I supplied.

What about a simple 'string.match("...")' for a check if string contains only digits?

@rodolfo
In this thread, and in your other thread about centering lines,
you used pretty advanced GUI stuff like document filters and printJobs.
So, if you are asking me how to add a JTextField to a JPanel, while I
have given quite some examples now of how to get a NumericField
into your panels, that I cannot help to think you mean something
different with this question.

So I have a request: can you zip your project (i.e. the folder that contains
all of your code, including possibly some other resources), put in on
Git, DropBox. Google Drive or similar, so that I can have a look at
what you have so far?



What I meant with this "Also can you enlighten me on how you manage to add a jtextfield on that panel?", Is that example of yours which when executed will create a textfield in a panel which confused me since I cannot find in your code a line that creates a jtextfield.

Scratch this:
Also regarding the new initMyOwnVariables you provided when I tried it my panel was empty.

your code worked but as I suspected the Jtexfield was hidden behind my Jlabel. Is there anyway for it to change its position the structure of the jpanel is like this:

card1
-jtextfield
-jbutton
-jlabel - background image

after I add your code and run it:
card1
-jbutton
-jlabel - background image
-jtextfield

I was hoping that there would be a way to move jtextfield up so that it would not be hidden behind my background image. Thank you once again
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:It is indeed supposed to be a numeric check, and no, it is not wrong.
Please have a close look at the code I supplied.


Ah, gotcha. My apologies. It was the name 'str' that caught me out.

What about a simple 'string.match("...")' for a check if string contains only digits?


Quite right. I was overthinking (it still happens ).

There is one really nice feature of verify() though: When it "fails", it tells you where it failed, which allows you to pass that information back to a user. I often wish that Matcher could do the same thing for a regex, but it's actually extremely difficult (as discussed here).

However, we're drifting off-topic, so I'll keep my nose out of GUI stuff in future....Well, maybe.

Winston
 
Piet Souris
Saloon Keeper
Posts: 5583
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rodolfo tuble wrote:(...)
I was hoping that there would be a way to move jtextfield up so that it would not be hidden behind my background image. Thank you once again


hi rodolfo,

yes, you are absolutely right. I seldomly work with a null layout, and I have no
experience when it comes to overlapping components. So I wrote a small
program to test it out. And I learned two or three things that I did not
know before!

The key to a correct ordering is the Z-Ordering property. When using a null
layout, the component added last is printed first. So to get a background,
and then the textfield and the button, the order to add these components
to the panel is:


Here is my try-out program. To get a decent background, not too big,
I loaded an image which was way too big, and made a scaled down
version. Well, that is not important.

Look at the z-orders that I am printing. The one with the highest order
is printed first.
Then notice how I manage to replace the textfield by some JLabel (just
click the button), and still manage to get that label printed on top of the
background. The code is in the method 'switchComponents'.

So, please look at my demo code. It is not the neatest code I've ever
written, Anyway: I myself learned a couple of things now. Never too old
to learn!

But: if anyone has some better ways to do all of this: let us know!
 
Piet Souris
Saloon Keeper
Posts: 5583
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:(...)


hi Winston,

you are actually right: the code I supplied is not, eh, very neat!
But I wanted it to limit it to only one character for now (you can get more
when pasting characters in) and since it was only a demo, I did
not spend much time to this.

And rightly so... the problem of getting an object of this class in its correct
position turned out to be, what you mentioned in a rahter poetic way, 'mushroomed'
not to say 'nightmarish'.

But I'll keep your method 'verify()' in mind: it seems very usefull indeed.
 
Piet Souris
Saloon Keeper
Posts: 5583
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi rodolfo,

well, I think the end is near. It is quite a thing to replace
your textfields by these NumericFields, in the way described.

So I turned back to your original question about more than one
filter. I had tried this before, but I couldn't get it to work.
Yesterday evening I saw what I did wrong: I had only done
an override of the 'insert' method, but it turned out that java
uses the 'replace' method.

So, here is an updated class 'SizeAndDigitFilter', and you only
need to replace the filter of your original JTextField by an
instance of this class. I have included a demo how to do this.

I've tested it, but it seems okay.

 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi rodolfo,

well, I think the end is near. It is quite a thing to replace
your textfields by these NumericFields, in the way described.

So I turned back to your original question about more than one
filter. I had tried this before, but I couldn't get it to work.
Yesterday evening I saw what I did wrong: I had only done
an override of the 'insert' method, but it turned out that java
uses the 'replace' method.

So, here is an updated class 'SizeAndDigitFilter', and you only
need to replace the filter of your original JTextField by an
instance of this class. I have included a demo how to do this.

I've tested it, but it seems okay.



It seems it really is the end for this problem, I cannot thank you enough for your contribution and for the knowledge you have imparted with me, I do hope that you also gained a few things in this endeavour of ours and that I will see you again in the near future. Also, your knowledge and the thirst for more knowledge is really top notch! I hope I can be as good as you in the future. thank you once again, time for me to work on the other parts of this system.

Thank you Piet
 
Piet Souris
Saloon Keeper
Posts: 5583
213
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi rodolfo,

you're most welcome! And rest assured: I learned quite a few things myself
(never worked witk Filters and Z-orders before ) and I did it with much
pleasure.

Success with your project!
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic