Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2004

Tip: Looking for answers? Try searching our database.

How to capture input of JTextField

Thread view: 
Nick H - 07 Feb 2004 21:23 GMT
Hello,
    I want to be able to capture the input of a JTextField and do things
based on what key was pressed. Basically this is for a textfield that
will allow the user to type in a value in pence, and for the number to
come up in GBP in a "£x.yz" format. I've looked at Inputmap and
Actionmap and I can get them to do things when I pressed a certain
key... but it still enters the text into the box and I have to set it up
for each key. Is it possible to intercept the key presses and use them
before the textfield does?

Thanks

Signature

Nick H

ak - 07 Feb 2004 23:58 GMT
> I want to be able to capture the input of a JTextField and do things
> based on what key was pressed. Basically this is for a textfield that
[quoted text clipped - 4 lines]
> for each key. Is it possible to intercept the key presses and use them
> before the textfield does?
better you use Document to control input.

____________

http://reader.imagero.com the best java image reader.
hiwa - 08 Feb 2004 09:41 GMT
> Hello,
>     I want to be able to capture the input of a JTextField and do things
[quoted text clipped - 7 lines]
>
> Thanks

KeyListener
Nick H - 09 Feb 2004 18:39 GMT
>>Hello,
>>    I want to be able to capture the input of a JTextField and do things
[quoted text clipped - 9 lines]
>
> KeyListener

Hi,
I just tried that but it still enters the keys typed into the box. I
want to be able to control the box.
ak - 09 Feb 2004 22:44 GMT
> > KeyListener
>
> Hi,
> I just tried that but it still enters the keys typed into the box. I
> want to be able to control the box.

JTextField field = new JTextField(new MyDocument());

class MyDocument extends PlaintDocument {
   public void insertString(int offset, String str, AttributeSet a) {
       //check here your staff
       if(<str is not ok>) {
           return;
       }
       else {
           super.insertString(offset, str, a);
       }
   }
}

____________

http://reader.imagero.com the best java image reader.
Nick H - 11 Feb 2004 10:06 GMT
>[code and stuff]

Thanks ak, I used some of yours and some stuff from the Sun website and
got it to work! Great stuff

Signature

Nick H

Nick Howes - 11 Feb 2004 10:57 GMT
>> [code and stuff]
>
> Thanks ak, I used some of yours and some stuff from the Sun website and
> got it to work! Great stuff

Hmm, maybe not...

I use the method to check that the input is a number key, and then add
that to an internal string. Then I want the box text to be set to
something else based on that (a currency-formatted string). The problem
is that insertString only appends text and I want it to replace the
existing text after every key-press.
So I tried using the super.replace() method, but I think that uses
insertString so it starts infinitely recursing til there's a stack
overflow.

Any ideas?

Signature

Nick H

ak - 11 Feb 2004 16:53 GMT
> I use the method to check that the input is a number key, and then add
> that to an internal string. Then I want the box text to be set to
[quoted text clipped - 4 lines]
> insertString so it starts infinitely recursing til there's a stack
> overflow.

you should use super.insertString().

may be you also coulduse JFormattedTextField.

--

____________

http://reader.imagero.com the best java image reader.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.