Virtual keyboard in a JFrame

1

Good friends

I need help with a form that uses a virtual keyboard, this keyboard is in jPanel , at the moment I have only been able to program it so that the text is entered in a single jTextFIeld , I am looking for the way that the keyboard can write in any of the jTextField that I have put in the form.

I hope to have explained well and beforehand thank you very much!

View of the jFrame

private void b1ActionPerformed(java.awt.event.ActionEvent evt){
    String A = campo1.getText();
    campo1.setText(A + "1");
}                               

this is the code that you assign to each button when you click on the

    
asked by Armando Escobedo 13.07.2017 в 09:30
source

1 answer

1

In my opinion I would use a control variable that indicates that jTextFIeld has supposedly the focus.

When you click on a button on the virtual keyboard, you would switch that variable to find out what jTextFIeld write.

I have Java a bit rusty and I do not remember the name of the events for when a component receives the focus, but to see if with this example you can get an idea of what I'm saying:

String fieldActivo = "jTextField1";

private void jTextField1_Focus() {
    fieldActivo = "jTextField1";
}

private void jTextField2_Focus() {
    fieldActivo = "jTextField2";
}

private void jTextField3_Focus() {
    fieldActivo = "jTextField3";
}

private void jTextField4_Focus() {
    fieldActivo = "jTextField4";
}

private void b1ActionPerformed(java.awt.event.ActionEvent evt){

    JTextField field;
    switch (fieldActivo)
    {
        case "jTextField1": field = jTextField1; break;
        case "jTextField2": field = jTextField2; break;
        case "jTextField3": field = jTextField3; break;
        case "jTextField4": field = jTextField4; break;
    }

    String A = field.getText();
    field.setText(A + "1");
}
    
answered by 13.07.2017 / 10:10
source