How to make background frame's JtextField focused?

143 Views Asked by At

I am stuck with a problem. I have designed a simple JFrame with 2 textboxes. At the time of loading the JFrame I also load VirtualKeyboard which is a JDialog form. The problem is now that both, the frame and the keybord, are there but in the JFrame I am not able to click on the textfields. If I close the keybord I am able to use the JFrame. How can I access the JFrame when the keyboard is opened.

When the JFrame loads I am calling the JDialog as follows_

This is my JFrame:

public class TestText extends javax.swing.JFrame {

static KeyBoard vk;

/**
 * Creates new form TestText
 */
public TestText() {
    initComponents();

    vk = new KeyBoard(new javax.swing.JFrame(), true);
}

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new TestText().setVisible(true);
             vk.setLocation(30,500);
            vk.setVisible(true);
        }
    });
}

private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;

}

This is my VirtualKeybord which is a JDialog:

public class KeyBoard extends javax.swing.JDialog {

/**
 * Creates new form KeyBoard
 */
public KeyBoard(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    setFocusableWindowState(false);
    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    initComponents();
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if(evt.getSource()==jButton2)
    {
        try{
            Robot robot = new Robot(); 
            robot.keyPress(KeyEvent.VK_A); 
            }
            catch(Exception E){}
    }
} 
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     if(evt.getSource()==jButton1)
    {
        try{
            Robot robot = new Robot(); 
            robot.keyPress(KeyEvent.VK_TAB); 
            }
            catch(Exception E){}
    }
}  
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;

What do I need to change, that it is possible to access both, the keyboard and the JFrame with the textfields?

1

There are 1 best solutions below

2
On BEST ANSWER

the problem is that you set the new frame as the parent of your KeyBoard Dialog. If you do that, the keyboard blocks the jframe until it's closed. There are multiple ways to solve that.

1) You can set modal to false. This tells the dialog that it should not block its parent:

vk = new KeyBoard(new javax.swing.JFrame(), false);

2) You can init the keyboard with null as its parent and start the frame seperately:

public TestText() {
    initComponents();
    new javax.swing.JFrame();
    vk = new KeyBoard(null, true);
}

This creates a new frame independent of your keyboard.

3) Another way is, if you want, that your keyboard knows the before created jframe you must add it to a different variable than the parent, like this:

public class KeyBoard extends javax.swing.JDialog {

    /**
     * Creates new form KeyBoard
     */
    public KeyBoard(java.awt.Frame parent, boolean modal) {
        super(null, modal);

        this.frame = parent;

        setFocusableWindowState(false);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        initComponents();
    }

    //Here are the other methods...

    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private java.awt.Frame frame;
}

Then you're also able to create a new unblocking keyboard like this:

vk = new KeyBoard(new javax.swing.JFrame(), false);