Tab traversal

Hi,

I'm currently working on a virtual keyboard that allows the user to traverse the textfields using the tab button and came across an interesting problem. The first key pressed from my virtual keyboard after virtual keyboard tab traversal seems to be ignored. However, subsequent key presses are printed on the textfield. Here's a sample program to illustrate the problem.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.text.JTextComponent;

publicclass Testextends JFrameimplements FocusListener

{

private JTextField one =null;

private JTextField two =null;

private JButtonbutton =null;

private JButtonsendKeyEvent =null;

private JDialogdialog =null;

private JTextComponent m_targetComponent =null;

public Test()

{

JPanel buttonPanel =new JPanel();

dialog =new JDialog();

dialog.setSize(100, 100);

dialog.setFocusable(false);

sendKeyEvent =new JButton("Send Key Event");

sendKeyEvent.setFocusable(false);

sendKeyEvent.addActionListener(new ActionListener()

{

publicvoid actionPerformed(ActionEvent e)

{

m_targetComponent.dispatchEvent(new KeyEvent(m_targetComponent,

KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0,

KeyEvent.VK_UNDEFINED,'a'));

}

});

buttonPanel.add(sendKeyEvent);

JButton tab =new JButton("tab");

tab.setFocusable(false);

tab.addActionListener(new ActionListener()

{

publicvoid actionPerformed(ActionEvent e)

{

m_targetComponent.dispatchEvent(

new KeyEvent(m_targetComponent,

KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0,

KeyEvent.VK_TAB, KeyEvent.CHAR_UNDEFINED));

}

});

tab.setFocusable(false);

buttonPanel.add(tab);

dialog.add(buttonPanel);

dialog.pack();

button =new JButton("display");

button.addActionListener(new ActionListener()

{

publicvoid actionPerformed(ActionEvent e)

{

dialog.setVisible(true);

}

});

one =new JTextField();

one.addFocusListener(this);

one.setPreferredSize(new Dimension(50, 50));

two =new JTextField();

two.addFocusListener(this);

two.setPreferredSize(new Dimension(50, 50));

JPanel panel =new JPanel();

panel.add(one);

panel.add(two);

panel.add(button);

getContentPane().add(panel);

pack();

setVisible(true);

}

publicvoid focusGained(FocusEvent e)

{

m_targetComponent = (JTextComponent)e.getSource();

}

publicvoid focusLost(FocusEvent e)

{

// TODO Auto-generated method stub

}

publicstaticvoid main(String[] args)

{

new Test();

}

}

Here are the steps to make the problem reoccur:

1. Click the "display pseudo keyboard" button

2. Click tab to shift the focus from textfield one to two

3. Click the "send key event" (first press is actually ignored, second is not).

Is there anything I'm doing wrong?

What's strange is, if I ispatch a Key_RELEASED event after

dispatching the tab key's KEY_PRESSED event, the

first key press after tab traversal is no longer ignored.

I'm running the code on JDK version 1.6.0_01-b06

Thanks for the help.

[6046 byte] By [Le0a] at [2007-11-27 9:02:27]
# 1

Your code does't even work at all on JDK1.4.2 using XP. That is no text is ever inserted into the text field.

I find using low level API a pain. You should always try to use higher level API's. You have a better chance that it will work on all platforms.

For example, to add text to a text field you can simply get the Document of the text field and use the insertString(...) method.

To tranfser focus you can use the KeyboardFocusManager.

camickra at 2007-7-12 21:33:14 > top of Java-index,Desktop,Core GUI APIs...