currency formatting in a MaskFormater

C

CD1

hi guys!

i wanna create a JFormattedTextField that filters only currency values.
i did something like:

JFormattedTextField ftfField = new
JFormattedTextField(NumberFormat.getCurrencyInstance(myLocale));

but, i noticed that i have to type the currency symbol on the field!
but i dont want it! that means, using the default locale, if i wanna
type 25 dollars i HAVE to type "$25"!!! but i want it to be only "25".
my locale is another one (brazilian), but the problem is the same.

i've searched about this on this group, but i havent found a specific
doubt like mine. there were many topics about formatting string (as a
variable), but i wanna something to put on a JFormattedTextField (or
whatever component is better).

thank u all,
Cristian Deives

E-mail: "cristian deives * gmail!computer".replace(" ",
"").replace("*", "@").replace("!", ".").replace("puter", "")
 
R

Roland

hi guys!

i wanna create a JFormattedTextField that filters only currency values.
i did something like:

JFormattedTextField ftfField = new
JFormattedTextField(NumberFormat.getCurrencyInstance(myLocale));

but, i noticed that i have to type the currency symbol on the field!
but i dont want it! that means, using the default locale, if i wanna
type 25 dollars i HAVE to type "$25"!!! but i want it to be only "25".
my locale is another one (brazilian), but the problem is the same.

i've searched about this on this group, but i havent found a specific
doubt like mine. there were many topics about formatting string (as a
variable), but i wanna something to put on a JFormattedTextField (or
whatever component is better).

thank u all,
Cristian Deives

E-mail: "cristian deives * gmail!computer".replace(" ",
^^^^^^^
replace(char,char) in java.lang.String cannot be applied to
(java.lang.String,java.lang.String)
1 error ;-)
"").replace("*", "@").replace("!", ".").replace("puter", "")

You could remove the currency symbol placeholder ('¤' or '\u00A4') from
the pattern and then create a number format with this pattern. Something
like the following:
NumberFormat curFormat = NumberFormat.getCurrencyInstance();
if (curFormat instanceof DecimalFormat) {
String pattern = ((DecimalFormat) curFormat).toPattern();
System.out.println(pattern);
String patternWithoutCurSym = pattern.replaceAll("\u00A4","");
System.out.println(patternWithoutCurSym);
curFormat = new DecimalFormat(patternWithoutCurSym);
} else {
System.err.println("Warning: you need to type the currency symbol");
}
JFormattedTextField ftfField = new JFormattedTextField(curFormat);

Note, that toPattern() is not defined for NumberFormat, but in its
subclass DecimalFormat. NumberFormat.getCurrencyInstance() always seems
to return a DecimalFormat instance, though.


--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
C

CD1

Thanks, Roland! It worked!

Now I create a DecimalFormat object with a "#,##0.00" mask =)
(this mask was taken from what you said, I did a
getCurrencyInstance(my_locale), System.out.println'ed it, removed the
currency symbol and now I have this)

But this way I have to type a dot ".", and not a comma "," to separate
the numbers. My currency format is like "50,00" and not "50.00". What
format should I use? I don't understand this formatting.. with #'s and
0's together :( I tried putting a "," instead of the ".", but it didn't
work. And now? :(

PS: my e-mail string is right!! No errors here! Here it is again:

String my_email = "cristian deives * gmail!computer".replace(" ",
"").replace("*", "@").replace("!", ".").replace("puter", "");
 
C

CD1

Thanks, Roland! It worked!

Now I create a DecimalFormat object with a "#,##0.00" mask =)
(this mask was taken from what you said, I did a
getCurrencyInstance(my_locale), System.out.println'ed it, removed the
currency symbol and now I have this)

But this way I have to type a dot ".", and not a comma "," to separate
the numbers. My currency format is like "50,00" and not "50.00". What
format should I use? I don't understand this formatting.. with #'s and
0's together :( I tried putting a "," instead of the ".", but it didn't
work. And now? :(

PS: my e-mail string is right!! No errors here! Here it is again:

String my_email = "cristian deives * gmail!computer".replace(" ",
"").replace("*", "@").replace("!", ".").replace("puter", "");
 
R

Roland

Thanks, Roland! It worked!

Now I create a DecimalFormat object with a "#,##0.00" mask =)
(this mask was taken from what you said, I did a
getCurrencyInstance(my_locale), System.out.println'ed it, removed the
currency symbol and now I have this)

But this way I have to type a dot ".", and not a comma "," to separate
the numbers. My currency format is like "50,00" and not "50.00". What
format should I use? I don't understand this formatting.. with #'s and
0's together :(

Explained here:
I tried putting a "," instead of the ".", but it didn't
work.

In the _pattern_, "," always represents the grouping separator and "."
the decimal separator. The _actual_ separator characters depend, of
course, on the locale. They are represented by an instance of class
DecimalFormatSymbols. By creating DecimalFormatSymbols for your locale
and passing that, together with the pattern, to the DecimalFormat
constructor, you obtain the appropriate format.

OK, how about this:
//Locale myLocale = Locale.getDefault();
//Locale myLocale = new Locale("en", "US");
Locale myLocale = new Locale("pt", "BR");
NumberFormat curFormat = NumberFormat.getCurrencyInstance(myLocale);
if (curFormat instanceof DecimalFormat) {
String pattern = ((DecimalFormat) curFormat).toPattern();
String patternWithoutCurSym = pattern.replaceAll("\u00A4\\s", "");
DecimalFormatSymbols dfs = new DecimalFormatSymbols(myLocale);
curFormat = new DecimalFormat(patternWithoutCurSym, dfs);
} else {
System.err.println("Warning: you need to type the currency symbol");
}
JFormattedTextField ftfField = new JFormattedTextField(curFormat);

Note that the replaceAll() now also removes white space (\s) that follow
the currency symbol placeholder ('¤' or '\u00A4').
PS: my e-mail string is right!! No errors here! Here it is again:

String my_email = "cristian deives * gmail!computer".replace(" ",
"").replace("*", "@").replace("!", ".").replace("puter", "");

Yes, believe your email address is right. However I made a little joke
that it does not compile with Java. If you compile the expression
"cristian deives * gmail!computer".replace(" ","")
it will generate an error because the String class has no method replace
that takes to Strings as parameter, only the method replace with two
char parameters.
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
C

CD1

Thanks again Roland!! Now it's the way I want ;)

But, again with the e-mail thing...

I know you were making a joke about it, but I was saying "no errors"
because it compiled fine here! I just checked the Sun documentation
and, as you said, the String.replace method has (char, char) as
parameters!! And I didn't know that! :p But it compiles!!! :|

Is it a bug I just found out?!? My Java is 1.5 Update 01

Try this simple class...

public class Email {
public static void main(String[] args) {
String my_email = "cristian deives * gmail!computer".replace(" ",
"").replace("*", "@").replace("!", ".").replace("puter", "");
System.out.println("My e-mail is " + my_email);
}
}

It works with me.

Anyway, thanks again for the currency problem!

PS: I just wanted to make clear that my code, though wrong, is
compiling (and running)! I knew you were making a joke... ehhehhhe
Bye!!
 
R

Roland

Thanks again Roland!! Now it's the way I want ;)

Is it a bug I just found out?!? My Java is 1.5 Update 01 That explains it.
Try this simple class...

public class Email {
public static void main(String[] args) {
String my_email = "cristian deives * gmail!computer".replace(" ",
"").replace("*", "@").replace("!", ".").replace("puter", "");
System.out.println("My e-mail is " + my_email);
}
}

String.replace(CharSequence,CharSequence) is new in 1.5.
I was compiling with 1.4.2 which resulted in the err.

Oh well, no bug ;-)

--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
Joined
Feb 4, 2011
Messages
1
Reaction score
0
Alternative

If you want to have the jformattedtextfield to display the currency format and edit it as a number, do this:

1. create the field with the currency formatter.
jFormattedTextField1 = new javax.swing.JFormattedTextField(NumberFormat.getCurrencyInstance(new Locale("pt", "BR")));

2. Add a focus gained event listener. When the focus is gained, replace the format of the text.
private void jFormattedTextField1FocusGained(java.awt.event.FocusEvent evt) {
DecimalFormat format = new DecimalFormat("#.#", new DecimalFormatSymbols(new Locale("pt", "BR")));
format.setMaximumFractionDigits(2);
jFormattedTextField1.setText(format.format((Double)jFormattedTextField1.getValue()));
jFormattedTextField1.setCaretPosition(0);
}

3. Add an input verifier to the field after the initialization.

jFormattedTextField1.setInputVerifier(new InputVerifier() {

@Override
public boolean verify(JComponent input) {
boolean ret = false;
try {
String temp = jFormattedTextField1.getText().replaceAll(",", ".");
double value = Double.parseDouble(temp);
ret = true;
jFormattedTextField1.setValue(value);
}catch(NumberFormatException ex) {
ret = false;
}
return ret;
}
});

In my case the currency decimal separator is comma. If you use dot it is easier and modify properly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top