java - Type on keyboard right to left? -


I'm looking for a way to type from left to right from the keyboard though user types I want to value and formatting Are there.

I'm trying this.

  The public class manifold textfile implements TextChangeListener {Private Static last long serial VERSIONUID = 1 L; Private Final StringBuilder MONEY_FORMAT = New StringBuilder (); Public MoneyField () {setWidth ("20%"); SetValue ("0,00"); SetMaxLength (14); AddStyleName ("numeros"); SetTextChangeEventMode (TextChangeEventMode.EAGER); AddTextChangeListener (this); Select all (); } @ Override Public Wide Text Change (Text Change Event) {//000.000.000,00 Brazilian currency format (! Event.gate text) () .tim () IsEmpty ()) {if (event.getText (). Length () == 2) {MONEY_FORMAT.setLength (0); MONEY_FORMAT.append (event.getText ()); MONEY_FORMAT.insert (2, ","); } And if (event.getText (). Length () == 6) {MONEY_FORMAT.setLength (0); MONEY_FORMAT.append (event.getText ()); MONEY_FORMAT.insert (6, "."); } And if (event.getText (). Length () == 10) {MONEY_FORMAT.setLength (0); MONEY_FORMAT.append (event.getText ()); MONEY_FORMAT.insert (10, "."); } And {MONEY_FORMAT.setLength (0); MONEY_FORMAT.append (event.getText ()); }} SetValue (MONEY_FORMAT.toString ()); }}  

This code works, but the format looks like this: 00,000,000,000, the format here is from left to right, and I think the format looks: 000,000,000, 00 correct

How can you leave your code?

In many cases is problematic First of all, you are changing the text of the input field, while the user is still typing. This makes for a terrible user experience, because the user is very careful that he is not interrupting the number format you have typed during typing. Otherwise, your formatting algorithm will break.

Again, you do not pay attention to the text that you can get from TextChangeEvent , when the text already has some length, this is when the user '10 , 'Your text field displays '10,' when the user types 3 more numbers, e.g. '10, 123 'takes your algorithm string and adds a decimal point to the right, gives '10, 123'. This is the reason that you have ended up with your number format.

He said, you really should not pierce a formatting number string together. Readers of your code (which may be your own future) stare at your code and try to achieve it. are doing.

There is a better way Java.text.NumberFormat to formatting for you then you should change the value of the not text field, while the user typed Has been doing. Better put the label of NumberFormat in the label, which is shown next to MoneyField or below. After each TextChangeEvent you can feed the current input text again on NumberFormat .

As a tip about handling the value of money, you can look at it. A lot may be wrong in money amount;)


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -