Hello,

I have a textfield price. The user has to input a price(currency) into it. I would like to do something so that when the user types in a curreny it will automatically add the commas and the zero when needed.

If the user was to type in 1000000 it will go as 100,000 or 3.5 it will go as 3.50.

Can you give me a hint on how to achieve this?

Thank you

When exactly do you want to do that formatting? As the user types - when he exits the field - when he clicks OK? What if he exits the field, you re-format it, then he goes back to that field - will you re-instate the input as he typed it for him to edit? What happens if his input is invalid?

I'm not being difficult, it's just that the requirement constrains the solution...

Sorry for the lack of clarity.Formatting happens when the user exit the textfield. Each time the user exit the textfield. I want nothing to happen if the input is invalid(which is unlikely)

Edited 3 Years Ago by london-G

You could...
add a FocusListener to the field so you know when it gains & loses focus.
In the focusLost method you can get the text in the field, validate and format it as required (use String's format method, or a Formatter instance), and set the text.
It may be a good idea to keep the user's original text, and reinstate that in the focusGained method.

If I were doing this I'd define my own subclass of JTextField to encapsulate and re-use all the new functionality.

Edited 3 Years Ago by JamesCherrill

Alright thank you James. I am a bit confused with the String's format method or Formatter instance.

My question is how can I format the textfield?

Also will it be alright to use the key pressed event in Netbeans instead of the focus listener?

Get the field's text - now you have it in a String. Parse that as a number. Create a new String that's a formatted version of the number. Use that to set the field's text.
String's format method takes a String and a format spec, and returns a formatted version of the String. The format method is documented in the API doc for String,and the format specs are documented in the API doc for Formatter.
Key pressed events are useless to you if the user has a mouse - he can type some input then click the next field. That's why the lostFocus event is the one you need.

Edited 3 Years Ago by JamesCherrill: Forgot stage to convert text to number before formatting.

Crikey the people at stackover are so anal about receiving good rep they practically complete the OP's question all by themselves which leaves very little room to allow the OP to attempt learn for themselves.

Thanks mrKobel, there is a good explanation of formatting text fields in the link you have provided.

I get it now James, thanks for helping as always.

@iamthwee

  • in most cases you are right about SO +++,

  • in Swing area we'are strictly asking for an SSCCE, then answer should be an SSCCE too

  • here are (in contrast with SO) bunch of comments (accepted that isn't possible to post an commments here, but seems sometimes looks like as loonly hears:-) as an answers,

  • did not intend to judge each forum has its own specifics, every are quite different, every forums where I member of
    ________________________________________________________

  • back to the specific question, without (not intentionally jooking) from OP or suggest to use hammer

  • some questions are specific, then there is possible to suggest to OP two ways

    1. (too hard to make that like joking by my side) with inputmask, input verifiers and formatter (really road to the hell, bunch of useless code, why to use hammer for)

    2. there are only a fews posters (and to share its) with excelent knowledges about javax.swing.Text and TextAction != KeyBindings, then why wasting with time -> post link or wrote linked code

The linked examples seem (a) pretty heavyweight amount of code and (b) don't meet the original requirement of formatting on field exit. Here's a simple example of what I was suggesting -(this one just changes the field to upper case when you exit and restores the user's exact input text when you re-enter. Changing the format rather than upper-casing is a trivial extension to this)

class UCField extends JTextField implements FocusListener {
   // displays input as upper case whenever it does not have the focus

   private String original = "";

   {addFocusListener(this);}

   @Override
   public void focusGained(FocusEvent e) {
      setText(original);
   }

   @Override
   public void focusLost(FocusEvent e) {
      original = getText();
      setText(original.toUpperCase());
   }

}

Edited 3 Years Ago by JamesCherrill

This question has already been answered. Start a new discussion instead.