I have been unable to get a JComboBox to report any Key/Focus Events to
listeners.  I have checked the archives on the mailing list and the bug reports
and couldn't find the problem reported.  I have encountered the problem in both
the Windows and Linux JVM's so I'm wondering at this point whether its my
problem or the JVM's problem.  Can a JComboBox not have a Focus/Key
Listener?  If not, Why not?

Anyway, here is a small sample code that demonstrates my problem.

When the JComboBox Gains/Loses Focus or has key pressed while its in focus you
should get a little message like:

Component 0 Lost Focus
Component 0 Gained Focus
Component 0 Key Pressed

Well, you get the idea.
-----------------------

import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.Component;
import java.awt.FlowLayout;
import java.util.Vector;

public class testClass extends JFrame implements FocusListener,
                                                 KeyListener
{
        Vector 
                componentList = null;

        public testClass()
        {
                JPanel panel = new JPanel(new FlowLayout());

                componentList = new Vector();
                componentList.addElement(new JComboBox());
                componentList.addElement(new JTextField(10));

                for (int i = 0; i < componentList.size(); i++)
                {
((Component)componentList.elementAt(i)).addFocusListener(this);
((Component)componentList.elementAt(i)).addKeyListener(this);
                        panel.add((Component)componentList.elementAt(i));
                }
                this.getContentPane().add(panel);
                setSize(320,200);
                setVisible(true);
        }

        public void focusLost(FocusEvent e)
        {
                System.out.println("Component " +
                   componentList.indexOf(e.getSource()) + " Lost Focus");
        }

        public void focusGained(FocusEvent e)
        {
                System.out.println("Component " +
                   componentList.indexOf(e.getSource()) + " Gained Focus");
        }

        public void keyTyped(KeyEvent e)
        {
                System.out.println("Component " +
                    componentList.indexOf(e.getSource()) + " Key Typed");
        }

        public void keyPressed(KeyEvent e)
        {
                System.out.println("Component " +
                    componentList.indexOf(e.getSource()) + " Key Pressed");
        }

        public void keyReleased(KeyEvent e)
        {
                System.out.println("Component " +
                    componentList.indexOf(e.getSource()) + " Key Released");
        }

        public static void main(String args[])
        {
                testClass t = new testClass();
        }
}



----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to