Results 1 to 5 of 5
Thread: Got problem with JtextField
- 12-05-2008, 04:36 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 41
- Rep Power
- 0
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 !!
-
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.
-
My bad: If you are working with money, you should use a currency instance of a NumberFormat object. You get this like so:
You can also pass a Locale object as a parameter here if it is showing the wrong currency symbol.Java Code:private NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
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.
- 12-06-2008, 02:23 PM #4
Member
- Join Date
- Aug 2008
- Posts
- 41
- Rep Power
- 0
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. :)
-
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
-
SWT_AWT bridge ,jtextfield edit problem
By Sureshgurram in forum SWT / JFaceReplies: 3Last Post: 12-09-2008, 12:40 PM -
JtextField
By kashifu in forum Advanced JavaReplies: 2Last Post: 06-27-2008, 04:25 PM -
how to focus to another JTextfield?
By birdofprey in forum AWT / SwingReplies: 2Last Post: 04-09-2008, 01:08 PM -
Using Columns With JTextField
By The Evil Genius in forum AWT / SwingReplies: 1Last Post: 03-17-2008, 01:01 AM -
help with JTextfield
By gary in forum New To JavaReplies: 4Last Post: 07-11-2007, 01:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks