在动作事件之后将系统焦点设置为特定组件

时间:2013-11-20 16:32:37

标签: java swing setfocus

我想要的时候:

comboBox.getSelectedIndex() == 1

然后系统焦点应设置为我的textField1

这是我的代码:

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == comboBox) {
        if (comboBox.getSelectedIndex() == 1) {

            // set focus to textField1
        }
    }
}

3 个答案:

答案 0 :(得分:1)

component.requestFocusInWindow();

答案 1 :(得分:0)

尝试textField1.requestFocus();

答案 2 :(得分:-1)

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == comboBox) {
        if (comboBox.getSelectedIndex() == 1) {

            // set focus to textField1
            textField1.requestFocus();
        }
    }
}