Question

This is my current code and the showButton is now working, thanks to you, but unfortunately the totalButton is still not. Could you me help figure this out?

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import java.util.EventObject;

public class GUISS extends JFrame implements ActionListener
{

public static void main(String[] args)
{
new GUISS();
}
private JButton totalButton, showButton;
private JTextField productField,  productField2, productField3, productField4, productField5;
private JTextField priceField, priceField2, priceField3, priceField4, priceField5;
private JTextField quantityField, quantityField2, quantityField3, quantityField4, quantityField5;
private JTextField totalField, totalField2, totalField3, totalField4, totalField5, emptyField, totalAmountField;
static double num1 , num2, num3, num4, num5, ans, ans2;
static double CBOX1 = 25.00, CBOX2 = 25.00, CBOX3 = 39.00, CBOX4 = 25.00;

public GUISS()
{
    setLayout(new FlowLayout());
    setTitle("Mcdonalds");
    setSize(520,200);

    productField = new JTextField("Best Sellers", 10);
    productField. setEditable(false);

    productField2 = new JTextField("Burger Mcdo", 10);
    productField2.setEditable(false);

    productField3 = new JTextField("Hot Fudge Sundae", 10);
    productField3.setEditable(false);

    productField4 = new JTextField("CheeseBurger", 10);
    productField4.setEditable(false);

    productField5 = new JTextField("Regular McFries", 10);
    productField5.setEditable(false);

    priceField = new JTextField("Alacarte Price", 10);
    priceField.setEditable(false);

    priceField2 = new JTextField("P25.00", 10);
    priceField2.setEditable(false);

    priceField3 = new JTextField("P25.00", 10);
    priceField3.setEditable(false);

    priceField4 = new JTextField("P39.00", 10);
    priceField4.setEditable(false);

    priceField5 = new JTextField("P25.00", 10);
    priceField5.setEditable(false);





    quantityField = new JTextField("Quantity", 10);
    quantityField.setEditable(false);

    quantityField2 = new JTextField(10);

    quantityField3 = new JTextField(10);

    quantityField4 = new JTextField(10);

    quantityField5 = new JTextField(10);


    totalField = new JTextField("Total Price", 10);
    totalField.setEditable(false);

    totalField2 = new JTextField("P0.0", 10);
    totalField2.setEditable(false);

    totalField3 = new JTextField("P0.0", 10); 
    totalField3.setEditable(false);

    totalField4 = new JTextField("P0.0", 10);
    totalField4.setEditable(false);

    totalField5 = new JTextField("P0.0", 10);
    totalField5.setEditable(false);

    emptyField = new JTextField(10);
   // JButton showButton = new JButton("Show price");
    //JTextFieldtotalButton = new JButton("Total Amount");
    totalAmountField = new JTextField("P0.0", 10);
    totalAmountField.setEditable(false);
  JTextField CashField = new JTextField(10);


 totalButton = new JButton("Total Amount");
  showButton = new JButton("Show price");

    add(productField);
    add(priceField);
    add(quantityField);
    add(totalField);

    add(productField2);
    add(priceField2);
    add(quantityField2);
    add(totalField2);

    add(productField3);
    add(priceField3);
    add(quantityField3);
    add(totalField3);

    add(productField4);
    add(priceField4);
    add(quantityField4);
    add(totalField4);

    add(productField5);
    add(priceField5);
    add(quantityField5);
    add(totalField5);

    add(showButton);
    showButton.addActionListener(this);
    add(totalButton);
    totalButton.addActionListener(this);
    add(totalAmountField);
    setVisible(true);




    }

    public void actionPerformed ( ActionEvent e )
    {  

     System.out.println("Entered action performed");

    if(e.getSource() == showButton ){
        if(!quantityField2.getText().equals(emptyField.getText())){
            num1 = Double.parseDouble(String.valueOf(quantityField2.getText()));
            ans = num1 * CBOX1 ;
            totalField2.setText(String.valueOf("P" + ans));
        }
        if(!quantityField3.getText().equals(emptyField.getText())){
            num1 = Double.parseDouble(String.valueOf(quantityField3.getText()));
            ans = num1 * CBOX2 ;
            totalField3.setText(String.valueOf("P" + ans));
        }
        if(!quantityField4.getText().equals(emptyField.getText())){
            num1 = Double.parseDouble(String.valueOf(quantityField4.getText()));
            ans = num1 * CBOX3 ;
            totalField4.setText(String.valueOf("P" + ans));
        }
        if(!quantityField5.getText().equals(emptyField.getText())){
            num1 = Double.parseDouble(String.valueOf(quantityField5.getText()));
            ans = num1 * CBOX4 ;
            totalField5.setText(String.valueOf("P" + ans));
       }           
        else if(e.getSource() == totalButton){
        if(!totalField2.getText().equals(0)){
            num2 = Double.parseDouble(String.valueOf(totalField2.getText()));
        }
        if(!totalField3.getText().equals(0)){
            num3 = Double.parseDouble(String.valueOf(totalField3.getText()));
        }
        if(!totalField4.getText().equals(0)){
            num4 = Double.parseDouble(String.valueOf(totalField4.getText()));
        }
        if(!totalField5.getText().equals(0)){
            num5 = Double.parseDouble(String.valueOf(totalField5.getText()));
        }
        else {
        ans2 = num2 + num3 + num4 + num5 ;

        totalAmountField.setText(String.valueOf("P" + ans2));
    }
}
    }
}
}

thanks for helping, this code is very important for me

Was it helpful?

Solution

well the problem here is that you did not add an action listener to the button to listen to the events that are triggered by the button called showButton. You need to add the following to your code after adding the button.

public GUISS()
{
    ...//your code as is
    add(showButton);
    showButton.addActionListener(this);
    ...//the rest of your code as is
}

Also to make sure that you are entering the actionPerformed method try to print something at the start of the method and see that it is output to the console. The line of code I added is adding the actionListener to the current instance of GUISS. That means that the instance you create of GUISS will listen to the actions performed by the button showButton. For more information you can look at the link http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Another problem here is that you are creating a local variable called showButton as well and that's the button the action is being added to not the instance variable showButton, to solve this problem modify your code as below:

public GUISS() {
    //JButton showButton = new JButton("Show price"); 
}

basically comment out the local variable called showButton. With this line not being commented the if condition for the getSource will not give the instance variable so they will not be equal.

Finally you have a bug with the condition regarding the totalButton, the totalButton condition if e.getSource() == totalButton is placed inside the condition of e.getSource() == showButton and as such it will never get executed you should re factor the else if to be the else if of the if e.getSource() == showButton

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top