title: JTextArea and JButton. Set the focus on the button
body: <p>The following program shows a <code>JTextArea</code> and a "Close" <code>JButton</code>. I defined the button as the default button because I would like it to have the focus as soon as I open the window. However, when I run the program the button is highlighted but the focus is on the text field. </p>
  
  <p>How do I set the focus on the button and not the text area?</p>
  
  <pre><code>package test;
  
  import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.awt.FlowLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTextArea;
  import javax.swing.ScrollPaneConstants;
  import javax.swing.border.EtchedBorder;
  
  
  public class Test {
  
      static JPanel southPanel;
      static JButton closeButton;
      static JFrame frame;
      static JTextArea textArea;
      private static final Dimension REASON_AREA_SIZE = new Dimension(250, 50);
  
      public static void main(String args[]) {
          String title = "";                                                                                                                                                                             
          frame = new JFrame();
          frame.setLayout(new BorderLayout());
          frame.setSize(1000, 800);
          textArea = new JTextArea();
          textArea.setLineWrap(true);
          textArea.setWrapStyleWord(true);
          textArea.setEditable(true);
          JScrollPane lScrollPane = new JScrollPane(textArea);
          lScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
          lScrollPane.setPreferredSize(REASON_AREA_SIZE);
          frame.add(lScrollPane, BorderLayout.CENTER);
          frame.add(createSouthPanel(), BorderLayout.SOUTH);
  
          //Here the close button is defined by default, the focus should be on it
          setDefaultButton();
  
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.setTitle(title);                                                                                                                                                                          
          frame.setVisible(true);
      }
  
      private static JPanel createSouthPanel() {
          southPanel = new JPanel(
                  new FlowLayout(FlowLayout.RIGHT, 10, 10));
          southPanel.setBorder(new EtchedBorder(EtchedBorder.RAISED));
          addCloseButton(southPanel);
          return southPanel;
      }
  
      /**
       * Set the close button as default
       */
      protected static void setDefaultButton() {
          frame.getRootPane().setDefaultButton(closeButton);
      }
  
      private static void addCloseButton(JPanel pButtonsPanel) {
          closeButton = new JButton("Close");
          closeButton.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent pEvent) {
                  frame.dispose();
              }
          });
          pButtonsPanel.add(closeButton);
      }
  }
  </code></pre>