Results 1 to 2 of 2

Thread: KeyListener & JComponent

  1. #1

    Thread Starter
    Addicted Member Mrs Kensington's Avatar
    Join Date
    Sep 2001
    Location
    Dorset, UK
    Posts
    144

    KeyListener & JComponent

    I'm trying to create my own custom control and want to use a KeyListener with it. But when i press a key after clicking the control i get absolutely nothing.

    Any ideas?

    Thanx in advance!

    This is my test code...

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class Test extends JComponent implements KeyListener
    {
    	public Test()
    	{
    		this.addKeyListener(this);
    	}
    	
    	//Key Listener
    	public void keyTyped(KeyEvent e){}
    	public void keyPressed(KeyEvent e)
    	{
    		System.out.println("Key Pressed!");
    	}
    	public void keyReleased(KeyEvent e){}
    	
    	public static void main(String[] args)
    	{
    		JFrame myFrame = new JFrame("Calendar Test");
    		JPanel thePanel = new JPanel(new BorderLayout());
    		thePanel.add(new Test(), BorderLayout.CENTER);
    		myFrame.getContentPane().add(thePanel);
    		myFrame.setVisible(true);
    	}
    }
    Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!

  2. #2

    Thread Starter
    Addicted Member Mrs Kensington's Avatar
    Join Date
    Sep 2001
    Location
    Dorset, UK
    Posts
    144
    ok managed to work it out!

    Just so people know, you have to overide the isFocusTraversable() method and return true (or false when its disabled).

    so just add this code...
    Code:
    public boolean isFocusTraversable()
    {
        return true;
    }
    Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width