简体   繁体   中英

Update JTextField with calculation from JComboBox

In my code, I'm trying to post in the JTextField the totalPrice calculation as soon as a someone clicks on an option from either of the comboboxes. I don't want the names ie "pepperoni" but the actual total calculation. Right now I have to click the text box and press enter to update the data.

How to update JTextField with calculation from JComboBox ?

import java.awt.* ;

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

 class JPizza extends JFrame implements ActionListener
 {
    private static final long serialVersionUID = 1L;

    public static void main(String[] args)
        {
            JPizza myFrame = new JPizza();
            myFrame.setVisible(true);
        }

    private static final int WIDTH = 250;
    private static final int HEIGHT = 300;
    private static final int X_ORIGIN = 200;
    private static final int Y_ORIGIN = 100;

      private static final double smallPizzaPrice = 7.00;
      private static final double mediumPizzaPrice = 9.00;
      private static final double largePizzaPrice = 11.00;
      private static final double extraLargePizzaPrice = 14.00;
      private static final int smallPizzaSize = 0;
      private static final int mediumPizzaSize = 1;
      private static final int largePizzaSize = 2;
      private static final int extraLargeSize = 3;

      private static final double cheese = 0.0, pepperoni = 1.00, peppers = 1.00, sausage = 1.00, olives = 1.00;
      private static final int cheesePlace = 0, pepperoniPlace = 1, peppersPlace = 2, sausagePlace = 3, olivesPlace = 4;

      int states = 0 ;
      double totalPrice = 0.0;   
      double pizzaSizePrice = smallPizzaPrice ;
      double toppingPrice = 0.0;

    //Toppings & Panel
    JComboBox<String> pizzaTopping;
    JPanel comboboxPanel2;

    //Pizza Size & Panel
    JComboBox<String> pizzaSize;
    JPanel comboboxPanel;

    //Price Display Field and Panel
    JTextField calculate;
    JPanel messagePanel;

    public JPizza()
    {
        super("Tower of Pizza");      

        JLabel title = new JLabel("Tower of Pizza");
        add(title);

        pizzaSize = new JComboBox<String>();
        pizzaSize.addItem( "Small" );
        pizzaSize.addItem( "Medium" );
        pizzaSize.addItem( "Large" );
        pizzaSize.addItem( "Extra Large" );
        pizzaSize.setSelectedIndex(0);
        pizzaSize.addActionListener(this);

        pizzaTopping = new JComboBox<String>();
        pizzaTopping.addItem( "Cheese" );
        pizzaTopping.addItem( "Pepperoni" );
        pizzaTopping.addItem( "Peppers" );
        pizzaTopping.addItem( "Sausage" );
        pizzaTopping.addItem( "Olives" );
        pizzaTopping.setSelectedIndex(0);
        pizzaTopping.addActionListener(this);

        calculate = new JTextField(10);   
        calculate.addActionListener(this); 

        Container contentPane = getContentPane();
        GridLayout contentpaneLayout = new GridLayout(5,0,10,10);
        contentPane.setLayout(contentpaneLayout);

        comboboxPanel = new JPanel();
        GridLayout comboboxPanelLayout = new GridLayout(2,0);
        comboboxPanel.setLayout(comboboxPanelLayout);
        comboboxPanel.add(pizzaSize);
        contentPane.add(comboboxPanel);

        comboboxPanel2 = new JPanel();
        GridLayout comboboxPanelLayout1 = new GridLayout(2,0);
        comboboxPanel2.setLayout(comboboxPanelLayout1);
        comboboxPanel2.add(pizzaTopping);
        contentPane.add(comboboxPanel2);

        messagePanel = new JPanel();
        GridLayout messagePanelLayout = new GridLayout(2,0);
        messagePanel.setLayout(messagePanelLayout);
        messagePanel.add(calculate);
        contentPane.add(messagePanel);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT);
        setResizable(false);
    }


    public void actionPerformed(ActionEvent e)
    {            
        Object source = e.getSource();

                 int pizzaSiz = (int)pizzaSize.getSelectedIndex();  

                 if (source == calculate){

                 if (pizzaSiz == smallPizzaSize){                  
                    pizzaSizePrice = smallPizzaPrice;                   
                  }
                 else if (pizzaSiz == mediumPizzaSize){
                      pizzaSizePrice = mediumPizzaPrice ;

                  }
                 else if (pizzaSiz == largePizzaSize){
                      pizzaSizePrice =  largePizzaPrice;
                  }
                 else if (pizzaSiz == extraLargeSize){
                     pizzaSizePrice =  extraLargePizzaPrice;
                  }

                 int pizzaTop = (int)pizzaTopping.getSelectedIndex(); 

                 if(pizzaTop == cheesePlace ){
                     toppingPrice = cheese;
                 }
                 else if(pizzaTop == pepperoniPlace){
                     toppingPrice = pepperoni;
                 }
                 else if(pizzaTop == peppersPlace){
                     toppingPrice = peppers;
                 }
                 else if(pizzaTop == sausagePlace){
                     toppingPrice = sausage;
                 }
                 else if(pizzaTop == olivesPlace){
                     toppingPrice = olives;      
                 }

                 totalPrice = pizzaSizePrice;  
                 totalPrice +=toppingPrice;
                 calculate.setText("Total Price: $" + totalPrice);             
             }

    }
 } 

You are purposely limiting your code by restricting what the actionPerformed does with the if test:

if (source == calculate){

Get rid of it and the method will do the calculations any time it actionPerformed is invoked. You had better make sure though that both combo boxes have valid selections before trying to calculate. You can test them by getting the combo box's selected index and testing if they are >= 0 (not -1).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM