0

I have created the following screen with netbeans:

private void initComponents()
{
    java.awt.GridBagConstraints gridBagConstraints;

    panelWest = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    textFieldScore = new javax.swing.JTextField();
    textFieldScore.setColumns(3);
    textFieldCourseRating = new javax.swing.JTextField();
    textFieldCourseRating.setColumns(4);
    textFieldCourseSlope = new javax.swing.JTextField();
    textFieldCourseSlope.setColumns(3);
    comboBoxCourse = new javax.swing.JComboBox<>();
    dateChooserAddScoresDate = new com.toedter.calendar.JDateChooser();
    chckbxAddScoresNineHoleScore = new javax.swing.JCheckBox();
    chckbxAddScoresTournamentScore = new javax.swing.JCheckBox();
    btnAddScoresAdd = new javax.swing.JButton();
    btnAddScoreDelete = new javax.swing.JButton();
    btnAddScoreDelete.setVisible(false);        // Initially turn "Delete" button off
    panelSouth = new javax.swing.JPanel();
    btnAddScoresExit = new javax.swing.JButton();

    setLayout(new java.awt.BorderLayout());

    textFieldScore.requestFocusInWindow();
    java.awt.GridBagLayout panelWestLayout = new java.awt.GridBagLayout();
    panelWestLayout.columnWidths = new int[] {0, 15, 0, 15, 0, 15, 0, 15, 0};
    panelWestLayout.rowHeights = new int[] {0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0};
    panelWest.setLayout(panelWestLayout);

    jLabel1.setText("Score");
    jLabel1.setFocusable(false);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    panelWest.add(jLabel1, gridBagConstraints);

    jLabel2.setText("Date");
    jLabel2.setFocusable(false);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    panelWest.add(jLabel2, gridBagConstraints);

    jLabel3.setText("Course");
    jLabel3.setFocusable(false);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    panelWest.add(jLabel3, gridBagConstraints);

    jLabel4.setText("Course Rating");
    jLabel4.setFocusable(false);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 6;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    panelWest.add(jLabel4, gridBagConstraints);

    jLabel5.setText("Course Slope");
    jLabel5.setFocusable(false);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 8;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    panelWest.add(jLabel5, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(textFieldScore, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 6;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(textFieldCourseRating, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 8;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(textFieldCourseSlope, gridBagConstraints);

    comboBoxCourse.setEditable(true);
    comboBoxCourse.addActionListener(new java.awt.event.ActionListener()
    {
        public void actionPerformed(java.awt.event.ActionEvent evt)
        {
            comboBoxCourseActionPerformed(evt);
        }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(comboBoxCourse, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(dateChooserAddScoresDate, gridBagConstraints);
    dateChooserAddScoresDate.setDateFormatString("MM/dd/yy");

    chckbxAddScoresNineHoleScore.setText("Nine Hole Score");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 4;
    gridBagConstraints.gridy = 0;
    panelWest.add(chckbxAddScoresNineHoleScore, gridBagConstraints);

    chckbxAddScoresTournamentScore.setText("Tournament Score");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 6;
    gridBagConstraints.gridy = 0;
    panelWest.add(chckbxAddScoresTournamentScore, gridBagConstraints);

    btnAddScoresAdd.setText("Add");
    btnAddScoresAdd.addActionListener(new java.awt.event.ActionListener()
    {
        public void actionPerformed(java.awt.event.ActionEvent evt)
        {
            btnAddScoresAddActionPerformed(evt);
        }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 14;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(btnAddScoresAdd, gridBagConstraints);

    btnAddScoreDelete.setText("Delete");
    btnAddScoreDelete.addActionListener(new java.awt.event.ActionListener()
    {
        public void actionPerformed(java.awt.event.ActionEvent evt)
        {
            btnAddScoreDeleteActionPerformed(evt);
        }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 16;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
    panelWest.add(btnAddScoreDelete, gridBagConstraints);

    add(panelWest, java.awt.BorderLayout.CENTER);

    btnAddScoresExit.setIcon(IconUtils.getNavigationIcon("Back", 24));
    btnAddScoresExit.setText("Back");
    btnAddScoresExit.addActionListener(new java.awt.event.ActionListener()
    {
        public void actionPerformed(java.awt.event.ActionEvent evt)
        {
            btnAddScoresExitActionPerformed(evt);
        }
    });
    panelSouth.add(btnAddScoresExit);

    add(panelSouth, java.awt.BorderLayout.PAGE_END);
}

That looks as following:

When the screen is displayed I want focus to be in the score field (textFieldScore). I am unable to do this. I have tried textFieldScore.requestFocusInWindow(); and textFieldScore.requestFocus;, but neither work. How can I get focus to the score input field?

***** EDITED *****

Originally, I had no frame.pack() statement. I added the following code after my last getContentPane().add(as, ADDSCORES); (for all my cards.

this.pack();
AddScores.textFieldScore.requestFocusInWindow();
this.setVisible(true);

This made no difference.

todivefor
  • 121
  • 9

2 Answers2

1

This is the second card displayed.

Sound like you are using a CardLayout. A CardLayout has a problem in that it does not place focus on the newly displayed panel.

Check out Card Layout Focus for an improved CardLayout that will set focus on the first component for you automatically.

If you don't want to use this code then you would need to set the focus on the component after you have changed cards and the panel is now visible on the GUI.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you that was it. Actually my question was a duplicate of [link] (http://stackoverflow.com/questions/6066642/get-focus-on-a-jtextfield-inside-a-cardlayout). Added addComponentListener and it is working fine. Thank you. – todivefor Sep 26 '16 at 12:08
  • @PeterReam, the solution presented here is a more general solution that will work for any CardLayout and works even when you have multiple panels. Using the approach from the above link works fine, but it means you need to keep writing a custom ComponentListener every time you use a CardLayout. – camickr Sep 26 '16 at 13:32
  • My CardLayout has 4/5 panels, 2 of which have textfields that require focus. I wrote 2 ComponentListeners to get it to work. I, now, want to look at "card layout focus" for my own edification. Thank you. – todivefor Sep 26 '16 at 19:31
  • just changed to using "card layout focus." It works great. I don't use any of the additional methods, but the focus on my input textfields works great. Thank you. – todivefor Sep 26 '16 at 21:55
0

It would be easier to tell if you would put all the code. Basically you need to make sure that you're requesting focus BEFORE the frame is displayed. So if you're initializing those components in a frame it should be

frame.pack();  
textFieldScore.requestFocusInWindow(); 
frame.setVisible(true);
freefall
  • 597
  • 2
  • 12
  • 28
  • I am doing before frame is displayed. I am using card layout. This is the second card displayed. Anything different? – todivefor Sep 25 '16 at 01:39
  • Are to components realized (pack()) before you're trying to set focus? – freefall Sep 25 '16 at 06:28
  • I had no pack() in my application. – todivefor Sep 25 '16 at 11:15
  • I had no pack() in my application. I added the following code:
    this.pack(); AddScores.textFieldScore.requestFocusInWindow(); this.setVisible(true);
    I applied this just after my last:
    getContentPane().add(as, ADDSCORES);
    cardlayout add.
    – todivefor Sep 25 '16 at 11:23