Results 1 to 5 of 5
  1. #1
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

    Default Got problem with JtextField

    I have a Quantity textField , TotalPrice textField and a variable price double type get value from database.
    When i insert quantity into Quantity TextField and click button Calculate , the TotalPricetextField.setText(String.valueOf(price * Integer.parseInt(QuantityTextField.getText)))
    the problem is : when totalprice has 8 digits like 10 000 000 , it will change to 1.0E7 and i cannot insert to database with this format.

    If i change TotalPriceTextfield to JFormattedText, i got an exception : Mutilpe Point !

    Help me solve this problem !!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You can format your displayed String using String.format(...) or using a DecimalFormat object, but the displayed format shouldn't affect database insertion as it's the number that's inserted, not its string representation.

    You may wish to post a small compilable program that demonstrates part of your problem.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    My bad: If you are working with money, you should use a currency instance of a NumberFormat object. You get this like so:
    Java Code:
    private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
    You can also pass a Locale object as a parameter here if it is showing the wrong currency symbol.

    The numeric result from your calculation should be stored separately and should be what is uploaded to the database, not the String from the totalprice jtextfield.

    Note that it is a very bad idea to represent currency with floating point variables as fishtoprecords correctly points out many times in this forum.
    Last edited by Fubarable; 12-05-2008 at 06:07 PM.

  4. #4
    hungleon88 is offline Member
    Join Date
    Aug 2008
    Posts
    41
    Rep Power
    0

    Default

    oh thanks !!!!, it's work , thanks !!!!!!
    i always insert to database with a text from texfield with Double.ParseDouble , Integer.ParseInt , ... so i forget i can insert with a variable. :)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Cool, I'm glad that you've got it working. Here is the app I used to test this:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.text.NumberFormat;
    import java.text.ParseException;
    
    import javax.swing.*;
    
    public class NumberFormattingApp
    {
      private JPanel mainPanel = new JPanel();
      private JTextField quantityField = new JTextField(10);
      private JTextField itemPriceField = new JTextField("$", 10);
      private JTextField totalPriceField = new JTextField(10);
      private int totalPriceTimes100 = 0;
    
      public NumberFormattingApp()
      {
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
        mainPanel.add(new JLabel("Quantity: "));
        mainPanel.add(quantityField);
        mainPanel.add(new JLabel(" "));
        
        mainPanel.add(new JLabel("Item Price: "));
        mainPanel.add(itemPriceField);
        mainPanel.add(new JLabel(" "));
    
        mainPanel.add(new JLabel("Total Price: "));
        mainPanel.add(totalPriceField);
        totalPriceField.setEditable(false);
        totalPriceField.setFocusable(false);
        mainPanel.add(new JLabel(" "));
        
        JButton calcButton = new JButton("Calculate");
        calcButton.addActionListener(new CalcListener());
        JPanel calcPanel = new JPanel(new BorderLayout());
        calcPanel.add(calcButton, BorderLayout.CENTER);
        mainPanel.add(calcPanel);
    
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
      
      private class CalcListener implements ActionListener
      {
        //TODO: make sure your locale is correct here
        private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
        
        public void actionPerformed(ActionEvent e)
        {
          try
          {
            int quantity = Integer.parseInt(quantityField.getText());
            
            // *** trying to practice what fishtoprecords preaches
            double price = currencyFormat.parse(itemPriceField.getText()).doubleValue(); 
            int intPrice = (int)(100 * price); // use my currency x 100 to represent money as int
            
            totalPriceTimes100 = intPrice * quantity;
            
            // to display divide by 100.0 and use currency format
            totalPriceField.setText(currencyFormat.format(totalPriceTimes100/100.0)); 
            
            System.out.println("send to Database my int total price x 100: " + totalPriceTimes100);
            System.out.println("If number may get big, may need to use long or BigDecimal or BigInteger");
          }
          catch (NumberFormatException nfe) 
          {
            quantityField.setText("");
            itemPriceField.setText("$");
            totalPriceField.setText("");
          }
          catch (ParseException pe)
          {
            quantityField.setText("");
            itemPriceField.setText("$");
            totalPriceField.setText("");
          }
        }
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("NumberFormattingApp");
        frame.getContentPane().add(new NumberFormattingApp().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

Similar Threads

  1. SWT_AWT bridge ,jtextfield edit problem
    By Sureshgurram in forum SWT / JFace
    Replies: 3
    Last Post: 12-09-2008, 12:40 PM
  2. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 04:25 PM
  3. how to focus to another JTextfield?
    By birdofprey in forum AWT / Swing
    Replies: 2
    Last Post: 04-09-2008, 01:08 PM
  4. Using Columns With JTextField
    By The Evil Genius in forum AWT / Swing
    Replies: 1
    Last Post: 03-17-2008, 01:01 AM
  5. help with JTextfield
    By gary in forum New To Java
    Replies: 4
    Last Post: 07-11-2007, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •