title: Inputting data to JTextField by pressing JButton
body: <p>I wrote a program that creates a sudoku board according to the difficulty level chosen by the user. There's a basic GUI using JFrame and JPanel.<br />
  The board itself is built using a 2D array of JTextFields to allow for editing by the user and I made a table of JButtons representing digits 1-9.<br />
  I'm trying to make it so when I press a digit button while my cursor is on the relevant text field, it'll input that number to the field.
  I think there's a problem with how I defined the buttons but would love a hand.</p>
  <pre><code>    /*Java Program to solve Sudoku problem using Backtracking*/
  import javax.swing.*;
  import javax.swing.border.Border;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import java.awt.*;
  
  
  public class Solver extends Board {
  
      Solver(int N, int K) {
          super(N, K);
      }
  
      private static void createWindow() {
          JFrame frame = new JFrame(&quot;Sudoku&quot;);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          createUI(frame);
          frame.setSize(250, 80);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
      }
  
      private static void createUI(final JFrame frame) {
          JPanel panel = new JPanel();
          LayoutManager layout = new FlowLayout();
          panel.setLayout(layout);
  
          JButton button = new JButton(&quot;Play&quot;);
          button.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  String result = (String) JOptionPane.showInputDialog(
                      frame,
                      &quot;Difficulty Glossary:\n\n Hard - 50/81 blank spaces\n Medium - 35/81 blank spaces\n Easy - 20/81 blank spaces\n\nChoose your desired difficulty:\n\tHard: 1\n\tMedium: 2\n\tEasy: 3\nIf your input doesn't match one of these digits, the board generated will be on easy mode.&quot;,
                      &quot;Difficulty Glossary&quot;,
                      JOptionPane.PLAIN_MESSAGE,
                      null,
                      null,
                      &quot;3&quot;
                  );
                  optionBoard();
                  play(Integer.parseInt(result));
              }
          });
  
          panel.add(button);
          frame.getContentPane().add(panel, BorderLayout.CENTER);
      }
      public static void optionBoard(){
  
      }
  
      public static void play(int level) {
          int N = 9, K = 0;
          switch (level) {
              case 1:
                  K = 50;
                  break;
              case 2:
                  K = 35;
                  break;
  
              default:
                  K = 20;
                  break;
          }
          Solver sudoku = new Solver(N, K);
          sudoku.fillValues();
          createBoard(sudoku.puzzle);
      }
  
      public static void createBoard(int[][] puzzle) {
          final Border fieldBorder = BorderFactory.createLineBorder(Color.BLACK);
  
          final JPanel grid = new JPanel(new GridLayout(9, 9));
          for (int i = 0; i &lt; 9; i++) {
              for (int j = 0; j &lt; 9; j++) {
                  final JTextField field = new JTextField(2);
                  if (puzzle[i][j] != 0) {
                      field.setText(puzzle[i][j] + &quot;&quot;);
                  } else {
                      field.setText(&quot;&quot;);
                  }
                  field.setHorizontalAlignment(JTextField.CENTER); //Center text horizontally in the text field.
                  field.setBorder(fieldBorder); //Add the colored border.
                  grid.add(field);
              }
          }
  
          final JPanel digits = new JPanel(new GridLayout(3, 3));
          int num=1;
          for (int i = 1; i &lt; 4; i++) {
              for (int j = 1; j &lt; 4; j++) {
                  final JButton digit = new JButton(num+&quot;&quot;);
                  num++;
                  
                  digits.add(digit);
              }
          }
  
          final JPanel centeredGrid = new JPanel(new GridBagLayout());
          centeredGrid.add(digits);
          centeredGrid.add(grid);
      
          final JFrame frame = new JFrame(&quot;Sudoku Board&quot;);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(centeredGrid);
          frame.setSize(400,400);
          frame.setVisible(true);
  
          JButton button = new JButton(&quot;Check&quot;);
          button.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  
              }
          });
  
         // centeredGrid.add(button);
      }
  
  
      // Driver code
      public static void main(String[] args) {
          createWindow();
      }
  }
  </code></pre>
  <p>I've only provided the relevant GUI class since the mathematical logic part of building the board is solid and works fine. That's what the Board class is.</p>