Trouble in entering numbers after decimal points - JFormattedTextField

Discussion in 'Java' started by kishanthany@gmail.com, Jun 26, 2006.

  1. Guest

    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();
    }
    }
     
    , Jun 26, 2006
    #1
    1. Advertising

  2. hiwa Guest

    ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:

    > 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();
    > }
    > }

    The most flexible way would be to use a DocumentFilter.
    Sorry I do not have enough time to write an example.
     
    hiwa, Jun 26, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Artek
    Replies:
    3
    Views:
    2,787
    Steven Cheng[MSFT]
    Feb 14, 2004
  2. Sam
    Replies:
    1
    Views:
    8,941
    Prnda76
    Feb 11, 2010
  3. Byte

    Working with decimal points

    Byte, Apr 8, 2006, in forum: Python
    Replies:
    8
    Views:
    423
    Fredrik Lundh
    Apr 8, 2006
  4. Guy Hocking

    Displaying data to 2 decimal points

    Guy Hocking, Mar 7, 2004, in forum: ASP General
    Replies:
    8
    Views:
    236
    Bob Barrows
    Mar 7, 2004
  5. Pp

    Precision upto n decimal points

    Pp, Dec 4, 2011, in forum: C Programming
    Replies:
    10
    Views:
    754
    gwowen
    Dec 5, 2011
Loading...

Share This Page