Results 1 to 3 of 3
  1. #1
    Steve78 is offline Member
    Join Date
    Dec 2012
    Location
    Miami
    Posts
    2
    Rep Power
    0

    Default Help need it, JComboBox to set price in calculator

    Newbie in need of help to finish calculator I got everything working except the JComboBox is not doing anything yet, every time i try code the combobox it turns in to a mess, this is what i need it to do // final result should be (number1) * (number2) / (144) * (setPrice) in ComboBox
    your help is appreciated,thank you in advance.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Calculator extends JFrame {
    
        JButton calculate;
        JTextField num1;
        JTextField num2; 
        JLabel result;
        JComboBox setPrice;
    
        public Calculator() {
    
            setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
    
    
            num1 = new JTextField(10);
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 1;
            c.gridwidth = 3;
            add(num1, c);
    
            num2 = new JTextField(10);
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 2;
            c.gridwidth = 3;
            add(num2, c);
            
            setPrice = new JComboBox(new String[] { "$9.99", "$10.99", "$11.99" });
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 3;
            c.gridwidth = 4;
            add(setPrice, c);
    
            calculate = new JButton("*");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 4;
            add(calculate, c);
            
            result = new JLabel("=" + "");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 5;
            c.gridwidth = 4;
            add(result, c);
          
            event a = new event();
            calculate.addActionListener(a);
    
    
        }
    
        public class event implements ActionListener {
            public void actionPerformed(ActionEvent a) {
    
                double number1, number2;
    
                try {
                    number1 = Double.parseDouble(num1.getText());
                } catch (NumberFormatException e) {
                    result.setText("Invalid entry");
                    result.setForeground(Color.red);
                    return;
                }
                try {
                    number2 = Double.parseDouble(num2.getText());
                } catch (NumberFormatException e) {
                    result.setText("Invalid entry");
                    result.setForeground(Color.red);
                    return;
    
                }
    
    //final result should be (number1) * (number2) / (144) * (setPrice) in ComboBox
                
                String op = a.getActionCommand();
                if (op.equals("*")) {
                    double factor = number1 * number2 / 144;
                    result.setText("= " + factor);
                    result.setForeground(Color.red);
                }
            }
        }
    }
    
    [CODE]
    
    import javax.swing.JFrame;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author Owner
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
           Calculator gui = new Calculator();
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setVisible(true);
            gui.setSize(300,200);
            gui.setTitle("testing set price by JComboBox");
            gui.setLocationRelativeTo(null);
        }
    }
    [/CODE]

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: Help need it, JComboBox to set price in calculator

    Here's what I suggest that you do to get your code working (I've actually tried this and it works but I don't want people to say that I'm spoon-feeding so I won't give you my solution).

    The problem that I see is with the dollar sign that you want to display in the GUI and you don't want the dollar sign when you are calculating the total amount. Here is my solution:

    1. Make a new class called "Price" (or some such thing). Define two methods. 1) getPrice() and 2) toString() You can later use getPrice() to get the number that you need for calculations and Swing can use the toString() method to convert the double to a String and your toString() method can add the dollar sign that you want to be there.
    2. Have a double as an instance variable inside of the Price class that you initialize when a Price object is created
    3. Declare an array of Price objects as an instance variable.
    4. Set each element of the array of Price objects with a Price object that you create by using one of your prices.
    5. When you call the constructor to JComboBox, instead of passing an array of String objects, pass an array of Price objects.
    6. In your actionPerformed() method, use the method getSelectedIndex() to give you the index of the Price object that was chosen, then use this index to get the Price object from the array of Price objects. Then, once you have your Price object, call getPrice() on it to get the price to do the calculations.

    That should do it.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Help need it, JComboBox to set price in calculator

    Quote Originally Posted by kaydell2 View Post
    1. Make a new class called "Price" (or some such thing). Define two methods. 1) getPrice() and 2) toString() You can later use getPrice() to get the number that you need for calculations and Swing can use the toString() method to convert the double to a String and your toString() method can add the dollar sign that you want to be there.
    2. Have a double as an instance variable inside of the Price class that you initialize when a Price object is created
    3. Declare an array of Price objects as an instance variable.
    4. Set each element of the array of Price objects with a Price object that you create by using one of your prices.
    5. When you call the constructor to JComboBox, instead of passing an array of String objects, pass an array of Price objects.
    Alternatively, populate the JComboBox with a Double[] array and use a custom renderer to prefix the dollar sign.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 04-17-2012, 05:33 AM
  2. Replies: 1
    Last Post: 12-23-2009, 12:24 AM
  3. Replies: 2
    Last Post: 12-10-2009, 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
  •