Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: KeyListener

  1. #1
    tim8w is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Cool KeyListener

    I have a JFrame to which I have added JButtons.

    Java Code:
    win=new JFrame("MyFrame");
    win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    win.setBounds(400,200,335,630);
    win.setResizable(false);
    win.setFocusable(true);
    		
    Container c=win.getContentPane();
    c.setBackground(Color.black);
    c.setFocusable(true);
    
    btnA = new JButton("A");
    btnA.setForeground(Color.white);
    btnA.setBackground(Color.black);
    
    c.add(btnA);
    Where do I add the KeyListener to? win or c? I've tried both and the code never calls keyPressed(), keyReleased() or keyTyped()...

    Any ideas on what I'm doing wrong?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: KeyListener

    Find and go through the Oracle tutorial on Key Bindings.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Need help with KeyListener
    By McDucky in forum New To Java
    Replies: 3
    Last Post: 09-16-2012, 05:35 PM
  2. Help with KeyListener
    By armyson in forum New To Java
    Replies: 1
    Last Post: 11-25-2011, 12:56 PM
  3. Help with keylistener?
    By Kaizo in forum New To Java
    Replies: 4
    Last Post: 12-11-2010, 12:55 AM
  4. keyListener not doing anything
    By imorio in forum AWT / Swing
    Replies: 10
    Last Post: 08-17-2010, 10:46 PM
  5. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 04:14 PM

Posting Permissions

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