title: How to use buttons to type in a text field?
body: <p>In a program I'm attempting to make, there are three text field accompanied by several buttons (each representing a number). I'm trying to get it so you can type by clicking on the buttons (as opposed to the keyboard). So far, I got this:</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  import java.awt.event.*;
  import javax.swing.text.*;
  
  public class GUI extends JFrame {
  
      private JButton[] numPad = new JButton[10];
      private JTextField totalBill = new JTextField();
      private JTextField totalPeople = new JTextField();
      private JTextField tipPercentage = new JTextField();
      private JTextField tipAmount = new JTextField();
      private JTextField grandTotal = new JTextField();
      private JTextField totalPerPerson = new JTextField();
      private JButton doneButton = new JButton("Done");
      private JButton clearButton = new JButton("Clear");
      ////////////////////////////////////////////////////////////////////////
      private JPanel superContainer;
      private JPanel container;
      private JPanel panel1 = new JPanel();
      private JPanel panel2 = new JPanel();
  
      public GUI() {
  
              //Set JFrame title.
              super("Tip Calculator");
  
              superContainer = new JPanel();
              superContainer.setLayout(new BoxLayout(superContainer, BoxLayout.Y_AXIS));
  
      //Create a container to hold two GridLayouts beside one another.
              container = new JPanel();
              container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
  
      //Create panels to be placed in container panel.
              panel1.setPreferredSize(new Dimension(500, 250));
              panel1.setLayout(new GridLayout(4,3,10,10));
              panel2.setPreferredSize(new Dimension(500, 250));
              panel2.setLayout(new GridLayout(7,2,10,10));
  
              //Populate all the JButtons for the numPad.
              for (int i=0; i&lt;=9; i++) {
                  numPad[i] = new JButton(Integer.toString(i));
              }
  
              //Place each numPad button on the first JPanel.
              for (int i=1; i&lt;=9; i++) {
                  panel1.add(numPad[i]);
              }
              panel1.add(numPad[0]);
  
              //Populate second GridLayout.
              panel2.add(new JLabel("Total Bill: "));
              panel2.add(totalBill);
              panel2.add(new JLabel("Total People: "));
              panel2.add(totalPeople);
              panel2.add(new JLabel("Total Percentage: "));
              panel2.add(tipPercentage);
              panel2.add(doneButton);
              panel2.add(clearButton);
              panel2.add(new JLabel("Tip Amount: "));
              panel2.add(tipAmount);
              panel2.add(new JLabel("Grand Total: "));
              panel2.add(grandTotal);
              panel2.add(new JLabel("Total/Person: "));
              panel2.add(totalPerPerson);
              grandTotal.setEditable(false);
              tipAmount.setEditable(false);
              totalPerPerson.setEditable(false);
  
  
              //Add the first GridLayout panel to the container.
              container.add(panel1);
              //Create a space between the GridLayout panels.
              container.add(Box.createRigidArea(new Dimension(30,0)));
              //Add the second GridLayout panel to the container.
              container.add(panel2);
  
              //Same as above but with title ontop and container panel below.
              superContainer.add(new JLabel("Title"));
              superContainer.add(Box.createRigidArea(new Dimension(0,30)));
              superContainer.add(container);
  
              //The panel the JFrame uses.
              this.setContentPane(superContainer);
  
              TheHandler handler = new TheHandler();
              doneButton.addActionListener(handler);
              clearButton.addActionListener(handler);
  
      }
  
      private class TheHandler implements ActionListener {
  
              public void actionPerformed(ActionEvent e) {
  
                     if (e.getSource()==doneButton) {
                              tipAmount.setText(Double.toString(Double.parseDouble(totalBill.getText()) * (Double.parseDouble(tipPercentage.getText()) / 100)));
                              grandTotal.setText(Double.toString(Double.parseDouble(tipAmount.getText()) + Double.parseDouble(totalBill.getText())));
                              totalPerPerson.setText(Double.toString(Double.parseDouble(grandTotal.getText()) / Double.parseDouble(totalPeople.getText()))); }
                      else if (e.getSource()==clearButton) {
                              grandTotal.setText("");
                              tipAmount.setText("");
                              totalPerPerson.setText("");
                              totalBill.setText("0");
                              tipPercentage.setText("0");
                              totalPeople.setText("1");
                              totalBill.requestFocus();
                              totalBill.selectAll(); }
  
              }
  
  
      }
  
  }
  </code></pre>
  
  <p>After searching the Internet, I came across the following code:</p>
  
  <pre><code>    private class AddDigit extends TextAction {
          private String digit;
  
          public AddDigit(String digit) {
  
              super( digit );
              this.digit = digit;
  
          }
  
          public void actionPerformed(ActionEvent e) {
  
              JTextComponent component = getFocusedComponent();
              component.replaceSelection( digit );
  
          }
  
      }
  </code></pre>
  
  <p>The only problem is, I have no clue how to use the code I found (the code I found being the second block).</p>
  
  <p><a href="https://stackoverflow.com/questions/8763695/add-text-to-a-text-field-via-jbuttons-when-there-are-two-textfields-calculator?rq=1">Where I got the code from.</a></p>