Hello There,
I have added a FocusListener to a JComboBox. But there is no event triggered on either focus gained or focus listened.
Any idea what could be wrong. I am using JDK1.3.1 on win2k
The code I am using is as follows..
public static void main(
String[] args)
{
JFrame jf= new JFrame();
Container contentPane = jf.getContentPane();
contentPane.setLayout(new FlowLayout());
JComboBox jc = new JComboBox();
jc.addItem("CEO");
jc.addItem("COO");
jc.addKeyListener(new ComponentKeyListener());
jc.addFocusListener(new ComponentFocusListener());
jf.addKeyListener(new ComponentKeyListener());
jf.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e)
{
System.out.println("Focus gained in Form");
}
public void focusLost(FocusEvent e)
{
JComponent jc = (JComponent)e.getSource();
System.out.println("LOST :Name is"+jc.getName());
}
});
JButton jb = new JButton("Check it out");
jb.addFocusListener(new ComponentFocusListener());
contentPane.add(jb);
contentPane.add(jc);
jf.setSize(600, 400);
jf.show();
}
static class ComponentFocusListener implements FocusListener
{
public void focusGained(FocusEvent e)
{
//System.out.println("Focus gained in Form");
}
public void focusLost(FocusEvent e)
{
JComponent jc = (JComponent)e.getSource();
System.out.println("LOST :x is"+jc.getBounds().x+" y is :"+jc.getBounds().y);
System.out.println("LOST :Name is"+jc.getName());
}
}
static class ComponentKeyListener implements KeyListener
{
public void keyPressed(KeyEvent event)
{
String key = KeyEvent.getKeyText(event.getKeyCode());
System.out.println("Key :"+ key);
}
public void keyReleased(KeyEvent ke)
{
//System.out.println("Key released in Form");
}
public void keyTyped(KeyEvent ke)
{
//System.out.println("Key typed in Form");
}
}