Article View: borland.public.jbuilder.java.swing
Article #178JFormattedTextField - Troble with decimal
From: kishanthany
Date: Mon, 26 Jun 2006 08:33
Date: Mon, 26 Jun 2006 08:33
85 lines
3382 bytes
3382 bytes
Hey!
I'm having problem with entering numbers after decimal point. I 've set
two numbers after decimal point. when I add first time it's working.
When try to override it takes third number & rounding it off and adding
it.
If I enable override mode enable when I entering number reaches cmmma
(,) with out problem but afterwards it takes next two digits after
decimal point addes with entered number.
I tried my best to solve it. please someone help me to sort out this
problem..
I've added my code here you can test and give me some idea..
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.text.NumberFormatter;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import javax.swing.text.DefaultFormatter;
import java.math.BigDecimal;
import java.util.Locale;
import java.text.*;
import javax.swing.text.MaskFormatter;
public class NumberCellEditor{
DecimalFormat numberFormat;
JFrame frame;
JFormattedTextField text,text1;
JPanel panel;
public void create(){
text=new JFormattedTextField();
text1=new JFormattedTextField();
numberFormat = (DecimalFormat) NumberFormat.getNumberInstance();
numberFormat.setDecimalSeparatorAlwaysShown(true);
numberFormat.setMinimumFractionDigits(2);
NumberFormatter numFormatter = new
NumberFormatter(numberFormat);
numFormatter.setAllowsInvalid(false);
numFormatter.setFormat(numberFormat);
//numFormatter.setOverwriteMode(true);
text.setValue(new Float(0.0F));
text.setFormatterFactory(new
DefaultFormatterFactory(numFormatter));
text.setHorizontalAlignment(JTextField.TRAILING);
}
public void createComp(){
frame=new JFrame(" TEST 2");
panel=new JPanel();
panel.setLayout(null);
create();
text.setBounds(100,100,100,30);
text1.setBounds(100,200,100,30);
panel.add(text);
panel.add(text1);
frame.getContentPane().add(panel);
frame.setSize(300,300);
frame.setVisible(true);
}
public static void main (String [] args){
NumberCellEditor n=new NumberCellEditor();
n.createComp();
}
}
Technical Details
Message-ID:
<1151310792.783040.46060@y41g2000cwy.googlegroups.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!postnews.google.com!y41g2000cwy.googlegroups.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 111d45,acff768aed5d21e2,start X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news2.google.com!postnews.google.com!y41g2000cwy.googlegroups.com!not-for-mail From: kishanthany@gmail.com Newsgroups: borland.public.jbuilder.java.swing Subject: JFormattedTextField - Troble with decimal Date: 26 Jun 2006 01:33:12 -0700 Organization: http://groups.google.com Lines: 86 Message-ID: <1151310792.783040.46060@y41g2000cwy.googlegroups.com> NNTP-Posting-Host: 202.69.193.40 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1151310796 24017 127.0.0.1 (26 Jun 2006 08:33:16 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 26 Jun 2006 08:33:16 +0000 (UTC) User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1),gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: y41g2000cwy.googlegroups.com; posting-host=202.69.193.40; posting-account=eqjSQg0AAADIuq05tcAQd0HNtC7GaDHV