title: How to declare variable to hold value of JTextField?
body: <p>I'm doing coding for Food Ordering GUI. I would like to ask few questions. I would like to ask that how should I declare variable to hold value for <code>tfPrice1</code>, <code>tfPrice2</code>, <code>tfPrice3</code>? What should I do to make the "Place Order" button so that when it is pressed it will sum up the values contained in the <code>JTextField</code>s? Below is my code.</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  public class FoodOrder1 extends JFrame
  {
      JButton riceBtn,noodleBtn,soupBtn;
      JTextField display,total,tfPrice1,tfPrice2,tfPrice3;
      JPanel mp,p1,p2,p3,p4,p5,p6,p7,p8;
      JLabel dsp,ttl,rLbl,nLbl,sLbl,prc1,prc2,prc3;
      int rice=3 , noodle=3 , soup=4;
      int Total , price1=Integer.parseInt(tfPrice1), price2=Integer.parseInt(tfPrice2) , price3=Integer.parseInt(tfPrice3);
      public FoodOrder1()
      {
          Container pane = getContentPane();
          mp = new JPanel();
          mp.setLayout(new GridLayout(14,1));
          pane.add(mp);
  
          p1 = new JPanel();
          p1.setLayout(new GridLayout(1,1));
          rLbl = new JLabel("Rice");
          rLbl.setFont(new Font("Myraid Pro",Font.BOLD,14));
          riceBtn = new JButton("Fried Rice " + "RM3");
          p1.add(riceBtn);
          riceBtn.addActionListener(new MyAction());
  
          p1.add(rLbl);
          p1.add(riceBtn);
  
          p2 = new JPanel();
          p2.setLayout(new GridLayout(1,2));
          nLbl = new JLabel("Noodle");
          nLbl.setFont(new Font("Myraid Pro",Font.BOLD,14));
          noodleBtn = new JButton("Tomato Noodle " + "RM3");
          noodleBtn.addActionListener(new MyAction());
  
          p2.add(nLbl);
          p2.add(noodleBtn);
  
          p3 = new JPanel();
          p3.setLayout(new GridLayout(1,2));
          sLbl = new JLabel("Soup");
          sLbl.setFont(new Font("Myraid Pro",Font.BOLD,14));
          soupBtn = new JButton("Tomyam Soup " + "RM4");
          soupBtn.addActionListener(new MyAction());
          p3.add(sLbl);
          p3.add(soupBtn);
  
          p4 = new JPanel();
          p4.setLayout(new GridLayout(1,2));
          prc1 = new JLabel("Price of Fried Rice");
          prc1.setFont(new Font("Myraid Pro",Font.BOLD,14));
          tfPrice1 = new JTextField(10);
  
          p4.add(prc1);
          p4.add(tfPrice1);
          tfPrice1.setEditable(false);
  
          p5 = new JPanel();
          p5.setLayout(new GridLayout(1,2));
          prc2 = new JLabel("Price of Tomato Noodle");
          prc2.setFont(new Font("Myraid Pro",Font.BOLD,14));
          tfPrice2 = new JTextField(10);
  
          p5.add(prc2);
          p5.add(tfPrice2);
          tfPrice2.setEditable(false);
  
          p6 = new JPanel();
          p6.setLayout(new GridLayout(1,2));
          prc3 = new JLabel("Price of Tomyam Soup");
          prc3.setFont(new Font("Myraid Pro",Font.BOLD,14));
          tfPrice3 = new JTextField(10);
  
          p6.add(prc3);
          p6.add(tfPrice3);
          tfPrice3.setEditable(false);
  
          p7 = new JPanel();
          p7.setLayout(new FlowLayout());
          poBtn = new JButton("Place Order");
          poBtn.setFont(new Font("Myraid Pro",Font.PLAIN,14));
          poBtn.addActionListener(new MyAction2());
          rstBtn = new JButton("Reset");
          rstBtn.setFont(new Font("Myraid Pro",Font.PLAIN,14));
          rstBtn.addActionListener(new MyAction3());
  
          p7.add(poBtn);
          p7.add(rstBtn);
  
          p8 = new JPanel();
          p8.setLayout(new GridLayout(1,2));
          ttl = new JLabel("Total (RM)");
          ttl.setFont(new Font("Myraid Pro",Font.BOLD,14));
          total = new JTextField(10);
  
          p8.add(ttl);
          p8.add(total);
          total.setEditable(false);
  
          mp.add(p1);
          mp.add(p2);
          mp.add(p3);
          mp.add(p4);
          mp.add(p5);
          mp.add(p6);
          mp.add(p7);
          mp.add(p8);
      }
  
      public class MyAction implements ActionListener
      {
          int counter=0;
          public void actionPerformed(ActionEvent e)
          {
              if (e.getSource() == riceBtn)
              {
                  counter++;
                  tfPrice1.setText("RM" + String.valueOf(counter*rice));
              }
  
              if (e.getSource() == noodleBtn)
              {
                  counter++;
                  tfPrice2.setText("RM" + String.valueOf(counter*noodle));
              }
  
              if (e.getSource() == soupBtn)
              {
                  counter++;
                  tfPrice3.setText("RM" + String.valueOf(counter*soup));
              }
          }
      }
  
      public class MyAction2 implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
          {
              price1.setText(String.valueOf(tfPrice1));
              price2.setText(String.valueOf(tfPrice2));           
              price3.setText(String.valueOf(tfPrice3));
  
              Total = price1+price2+price3;
              total.setText(String.valueOf(Total));
          }
      }
  
      public class MyAction3 implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
          {
              if(e.getSource() == rstBtn)
              {
                  tfPrice1.setText("");
                  tfPrice2.setText("");
                  tfPrice3.setText("");
                  total.setText("");
              }
          }
      }
  
      public static void main(String [] args)
      {
          FoodOrder1 f = new FoodOrder1();
          f.setVisible(true);
          f.pack();
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f.setSize(1000,800);
      }      
  }
  </code></pre>