focused component reference

前端 未结 3 1189
灰色年华
灰色年华 2021-01-22 22:16

I am working on a simple java swing based application. How would I get and set the text of the currently focused textfield/textarea of a form?

I know how to determine

3条回答
  •  无人共我
    2021-01-22 22:43

    Yes:

    Component focusOwner = ...
    if(focusOwner instanceof JTextComponent) { // a textfield or textarea is focused
        JTextComponent textComp = (JTextComponent) focusOwner;
        String s = textComp.getText();
    }
    

提交回复
热议问题