简体   繁体   中英

Set focus to input textfield

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);
}

如下所示:

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.

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM