I am trying to create a GUI app were the user inputs an, item name, price, and discount rate. The button should calculate the two numbers and display the discount price. I am stuck on the action listener and making it preform the action/calculation correctly.

I am a pretty novice Java developer, so any guidance is greatly appriciated. If someone could just provide a little insight in the right direction. I am pretty sure i'm heading in the right direction just not sure what I'm missing to make it work properly. My code is error free, compiles, and runs fine.

/*
 * 
 * 
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author Ash
 */
public class myGUI extends JFrame {                  

    public myGUI() {

        super("Retail Calculator");
        setLookAndFeel();
        JFrame frame = new JFrame();
        FlowLayout flow = new FlowLayout();
        frame.setLayout(flow);
        frame.setSize(350, 250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container container = getContentPane();
        JPanel panel1 = new JPanel();
        JLabel dlabel = new JLabel("Item Department:");
        String[] department = {"Women", "Men", "Boys", "Girls", "Infant"};
        JComboBox dept = new JComboBox(department);
        panel1.add(dlabel);
        panel1.add(dept);
        dept.setSelectedIndex(2);

        JPanel panel2 = new JPanel(new GridLayout(0, 2));
        JLabel item = new JLabel("Item name:");
        JTextField itemfield = new JTextField(10);
        panel2.add(item);
        panel2.add(itemfield);

        JPanel panel3 = new JPanel(new GridLayout(0, 2));
        JLabel price = new JLabel("Item price:");
        final JTextField pricefield = new JTextField(8);        
        panel3.add(price);
        panel3.add(pricefield);

        JPanel panel4 = new JPanel(new GridLayout(0, 2));
        JLabel discount = new JLabel("Item Discount:");
        final JTextField discountfield = new JTextField(8);
        panel4.add(discount);
        panel4.add(discountfield);



        JPanel panel5 = new JPanel(new FlowLayout());
        final JButton calc = new JButton("Calculate Discount");
        calc.setEnabled(true);     
        calc.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                double p= Double.parseDouble(pricefield.getText());
                double d= Double.parseDouble(discountfield.getText());
                double sum=0;

                while(p < sum && d < sum){
                    sum=p*d;
                 calc.setText(sum);   
                }                
            }
        });
        panel5.add(calc);      

        container.add(panel2, BorderLayout.NORTH);
        container.add(panel3, BorderLayout.CENTER);
        container.add(panel4, BorderLayout.SOUTH);


        frame.add(panel1);
        frame.add(container);
        frame.add(panel5);

        frame.setVisible(true);


    }

    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException exc) {
        }
    }


    public static void main(String[] args) {
        myGUI g = new myGUI();
    }
}
 @Override
 public void actionPerformed(ActionEvent ae) {
    double p = Double.parseDouble(pricefield.getText());
    double d = Double.parseDouble(discountfield.getText());
    double sum = 0;

    while (p < sum && d < sum) {
        sum = p * d;
        calc.setText(sum);   
    }
}

You initialize sum to 0, hence if you enter a value bigger than zero in your pricefield or discountfield, then the condition p < sum evaluates to false or d < sum evaluates to false, or both.
If at least one of the operands in a logical AND operation is false, then the condition evaluates to false, hence the statements in your loop won't be executed.
However, if you only need to display the product of both numbers, then you don't need a loop here at all, since p and d don't change, their product won't change either, so no point in looping.
If you need a check, then use an if statement. This loop will loop infinitely if you enter two negative numbers.

Edited 3 Years Ago by mvmalderen

Ok I realized I didn't have a way to display the discounted price in my program. I created another JTextField to hold the calculated discount price. However, I am not quiet getting how to link the textfield to calculation. any and all help appriciated.

/*
 * 
 * 
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author Ash
 */
public class myGUI extends JFrame {

//    double pricefield;
//    double discountfield;
//    double sum;
    public myGUI() {

        super("Retail Calculator");
        setLookAndFeel();
        JFrame frame = new JFrame();
        FlowLayout flow = new FlowLayout();
        frame.setLayout(flow);
        frame.setSize(350, 250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container container = getContentPane();
        JPanel panel1 = new JPanel();
        JLabel dlabel = new JLabel("Department:");
        String[] department = {"Women", "Men", "Boys", "Girls", "Infant"};
        JComboBox dept = new JComboBox(department);
        panel1.add(dlabel);
        panel1.add(dept);
        dept.setSelectedIndex(2);

        JPanel panel2 = new JPanel(new GridLayout(0, 2));
        JLabel item = new JLabel("Item name:");
        JTextField itemfield = new JTextField(10);
        panel2.add(item);
        panel2.add(itemfield);

        JPanel panel3 = new JPanel(new GridLayout(0, 2));
        JLabel price = new JLabel("Item price:");
        final JTextField pricefield = new JTextField(8);
        panel3.add(price);
        panel3.add(pricefield);

        JPanel panel4 = new JPanel(new GridLayout(0, 2));
        JLabel discount = new JLabel("Item Discount:");
        final JTextField discountfield = new JTextField(8);
        panel4.add(discount);
        panel4.add(discountfield);

        JPanel panel6 = new JPanel(new FlowLayout());
        final JTextField sumfield = new JTextField(5);
        panel6.add(sumfield);

        JPanel panel5 = new JPanel(new FlowLayout());
        final JButton calc = new JButton("Calculate Discount");
        calc.setEnabled(true);
        calc.addActionListener(new ActionListenerImpl( pricefield, discountfield, sumfield));



        panel5.add(calc);      

        container.add(panel2, BorderLayout.NORTH);
        container.add(panel3, BorderLayout.CENTER);
        container.add(panel4, BorderLayout.SOUTH);


        frame.add(panel1);
        frame.add(container);
        frame.add(panel5);
        frame.add(panel6);

        frame.setVisible(true);


    }

    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException exc) {
        }
    }


    private static class ActionListenerImpl implements ActionListener {

        private final JTextField pricefield;
        private final JTextField discountfield;
        private final JTextField sumfield;

        public ActionListenerImpl(JTextField pricefield, JTextField discountfield, JTextField sumfield) {
            this.pricefield = pricefield;
            this.discountfield = discountfield;
            this.sumfield = sumfield;
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            double p = Double.parseDouble(pricefield.getText());
            double d = Double.parseDouble(discountfield.getText());
            double sum = 0;

            while (p < sum && d < sum) {
                sum = p * d;              
            }
        }        
    }
        public static void main(String[] args) {
        myGUI g = new myGUI();
    }
}

Sorry I must not have refreshed my page before posting that last post. I am working your feedback now. I think that is probably the easiest way.I will update shortly.

Ok, I fixed my program and the event triggers but there is a problem in the event handling, my guess would be that i'm not passing something correctly (I tried for a bit to figure it out with no success). I changed from a nested class to an inner class declaration for my ActionListener. I had an issue with non-static and static methods and felt the inner class was easier for me to manage at this point. Here is the error I am getting:

> run:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Calculate Discount"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
    at java.lang.Double.parseDouble(Double.java:540)
    at myGUI$1.actionPerformed(myGUI.java:79)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:696)
    at java.awt.EventQueue$4.run(EventQueue.java:694)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD STOPPED (total time: 12 seconds)




    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    /**
     *
     * @author Ash
     */
    public class myGUI extends JFrame {


        double pf, df, sf;
        String p, d, s;
        private final JTextField discountfield;
        private final JTextField sumfield;
        private final JTextField pricefield;
        private final JButton calc;

        public myGUI() {

            super("Retail Calculator");
            setLookAndFeel();
            JFrame frame = new JFrame();
            FlowLayout flow = new FlowLayout();
            frame.setLayout(flow);
            frame.setSize(350, 250);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Container container = getContentPane();
            JPanel panel1 = new JPanel();
            JLabel dlabel = new JLabel("Department:");
            String[] department = {"Women", "Men", "Boys", "Girls", "Infant"};
            JComboBox dept = new JComboBox(department);
            panel1.add(dlabel);
            panel1.add(dept);
            dept.setSelectedIndex(2);

            JPanel panel2 = new JPanel(new GridLayout(0, 2));
            JLabel item = new JLabel("Item:");
            JTextField itemfield = new JTextField(10);
            panel2.add(item);
            panel2.add(itemfield);

            JPanel panel3 = new JPanel(new GridLayout(0, 2));
            JLabel price = new JLabel("Price:");

            pricefield = new JTextField(8);
            panel3.add(price);
            panel3.add(pricefield);

            JPanel panel4 = new JPanel(new GridLayout(0, 2));
            JLabel discount = new JLabel("Discount:");
            discountfield = new JTextField(8);
            panel4.add(discount);
            panel4.add(discountfield);

            JPanel panel6 = new JPanel(new FlowLayout());
            JLabel sum = new JLabel("Discounted Price:");
            sumfield = new JTextField(5);
            panel6.add(sum);
            panel6.add(sumfield);


            JPanel panel5 = new JPanel(new FlowLayout());
            calc = new JButton("Calculate Discount");
            calc.setEnabled(true);
            calc.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent ae) {


                        p = ae.getActionCommand();
                        pf = Double.parseDouble(p);
                        d = ae.getActionCommand();
                        df = Double.parseDouble(d);
                        s = ae.getActionCommand();
                        sf = Double.parseDouble(s);


                        sf = pf * df;
                        calc.setText(s); 
                    }

            });


            panel5.add(calc);

            container.add(panel2, BorderLayout.NORTH);
            container.add(panel3, BorderLayout.CENTER);
            container.add(panel4, BorderLayout.SOUTH);


            frame.add(panel1);
            frame.add(container);
            frame.add(panel5);
            frame.add(panel6);

            frame.setVisible(true);
        }                     
        private void setLookAndFeel() {
            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException exc) {
            }
        }
        public static void main(String[] args) {
            myGUI g = new myGUI();
        }

        }

Your error message is saying you are trying to convert "Calculate Discount" into an number and that's exactly what you are doing, 3 times.

p = ae.getActionCommand();
pf = Double.parseDouble(p);
d = ae.getActionCommand();
df = Double.parseDouble(d);
s = ae.getActionCommand();
sf = Double.parseDouble(s);

You are calling ae.getActionCommand() three times as if you expected a different value each time and expected that value to be a number, but you will get the same value repeatedly and that is the name of the action that caused this event. That's "Calculate Discount" every time because this action listener only listens for that button being clicked.

I think what you really want to do is get the text from your various fields and convert those strings into numbers. But you should use try{...}catch(NumberFormatException e){...} so that you can respond in a more friendly way if there is something other than a number in one of the fields.

I have had a difficult time implementing the try catch other than in the lookAndFeel which is pretty self explanitory.

What I need to do is take the user input from the textfields price and discount. Calculate them with the calculate discount button and display the discounted price. Would you mind without actually doing the work for me show me an example of something similar. That way I can create it myself with a decent model. I have had a hard time trying to go off of nothing and really work better with visual representation.

The following works pretty well for me. It is a tiny example that I think does the stuff that you are having trouble with. I hope it is self-explanatory.

import javax.swing.*;

public final class Example
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run() { init(); }
        });
    }
    public static void init()
    {
        final JFrame frame = new JFrame("Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JPanel panel = new JPanel(new java.awt.GridLayout(0,1));
        frame.add(panel);
        final JTextField fieldA = new JTextField();
        final JTextField fieldB = new JTextField();
        panel.add(fieldA);
        panel.add(fieldB);
        final JButton button = new JButton("Press me!");
        panel.add(button);
        button.addActionListener(new java.awt.event.ActionListener()
        {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent event)
            {
                String number = fieldA.getText();
                try
                {
                    double v = Double.parseDouble(number);
                    fieldB.setText(String.format("%3.2f", v * v));
                }
                catch(NumberFormatException e)
                {
                    fieldB.setText(number + " is not a number");
                }
            }
        });
        frame.pack();
        frame.setVisible(true);
    }
}

ALL RIGHT! Thank you kindly for your help. I am all squared away and working as it should. I appriciate your time and effort to help me out. Your example was exactly what I needed to wrap my mind around this concept. Here is my working code :)

/*
 * 
 * 
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Ash
 */
public class myGUI extends JFrame {


    double pf = 0.00, df = 0.00, sf = 0.00;
    String p, d, s;
    private final JTextField discountfield;
    private final JTextField sumfield;
    private final JTextField pricefield;
    private final JButton calc;

    public myGUI() {

        super("Retail Calculator");
        setLookAndFeel();
        JFrame frame = new JFrame("Retail Calculator");
        FlowLayout layout = new FlowLayout();
        frame.setLayout(layout);
        frame.setSize(450, 250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container container = getContentPane();
        JPanel panel1 = new JPanel();
        JLabel dlabel = new JLabel("Department:");
        String[] department = {"Women", "Men", "Boys", "Girls", "Infant"};
        JComboBox dept = new JComboBox(department);
        panel1.add(dlabel);
        panel1.add(dept);
        dept.setSelectedIndex(2);

        JPanel panel2 = new JPanel(new GridLayout(0, 2));
        JLabel item = new JLabel("Item:");
        JTextField itemfield = new JTextField(10);
        panel2.add(item);
        panel2.add(itemfield);

        JPanel panel3 = new JPanel(new GridLayout(0, 2));
        JLabel price = new JLabel("Price:");

        pricefield = new JTextField(8);
        panel3.add(price);
        panel3.add(pricefield);

        JPanel panel4 = new JPanel(new GridLayout(0, 2));
        JLabel discount = new JLabel("Discount:");
        discountfield = new JTextField(8);
        panel4.add(discount);
        panel4.add(discountfield);               

        JPanel panel5 = new JPanel(new GridLayout(0, 2));   
        sumfield = new JTextField(5);

        calc = new JButton("Calculate");
        calc.setEnabled(true);
        calc.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent ae) {

                p = pricefield.getText();
                d = discountfield.getText();

               try{

                pf = Double.parseDouble(p);
                df = Double.parseDouble(d);

                    double sum = pf *df;

                sumfield.setText(String.format("%3.2f", (pf-sum)));

               }catch(NumberFormatException e){

                sumfield.setText(pf +"or"+ df +"is not a valid number.");
               }
           }            
        });    

        panel5.add(calc);
        panel5.add(sumfield);    

        JPanel panel7 = new JPanel(new GridLayout(0, 2));
        JButton exit = new JButton("Exit");    // creates JButton                
        exit.addActionListener(new ActionListener() { // add an action listener with inner class to declare JButton event
            @Override
            public void actionPerformed(ActionEvent e) { //inner class assigns action to button
                   System.exit(0);
               }            
        });     
        panel7.add(exit);

        JPanel box = new JPanel();

        container.add(panel2, BorderLayout.NORTH);
        container.add(panel3, BorderLayout.CENTER);
        container.add(panel4, BorderLayout.SOUTH);
        box.add(panel5, BorderLayout.NORTH);
        box.add(panel7, BorderLayout.SOUTH);


        frame.add(panel1, BorderLayout.NORTH);
        frame.add(container, BorderLayout.CENTER);
        frame.add(box, BorderLayout.SOUTH);





        frame.setVisible(true);
    }                     
    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException exc) {
        }
    }
    public static void main(String[] args) {
        myGUI g = new myGUI();

    }     
    }
This question has already been answered. Start a new discussion instead.