Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: KeyListener

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool KeyListener

    I have a JFrame to which I have added JButtons.

    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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,365
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: KeyListener

    The component needs the focus to be able to receive key strokes.
    key binding is a more reliable way to receive key strokes.

    Post a small, complete program that compiles, executes and shows the problem. Make sure the code is properly formatted with indented statements that show nesting logic.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Re: KeyListener

    Norm,
    Here's a simple example. I have breakpoints in the debugger on all three key functions and they never get called:

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
     
     
    public class MyClass extends JFrame implements ActionListener, KeyListener
    {
    	static JFrame win;
    	static JButton btnAbout;
     
    	public static void main(String[] args)
    	{
    		win=new JFrame("MyFrame");
    		win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		win.setBounds(400,200,300,200);
    		win.setResizable(false);
    		win.setFocusable(true);
     
    		Container c=win.getContentPane();
    		c.setBackground(Color.black);
    		c.setLayout(null);
    		c.setFocusable(true);
    		Insets insets = c.getInsets();
    		c.addKeyListener(new MyClass());
     
    		btnAbout = new JButton("About");
    		btnAbout.setPreferredSize(new Dimension(80,30));
    		btnAbout.setForeground(Color.white);
    		btnAbout.setBackground(Color.blue);
    		btnAbout.setBounds(insets.left+100, insets.top+80,80,30);
     
    		c.add(btnAbout);
    		win.setVisible(true);
    }
     
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
     
    	}
    }

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: KeyListener

    Norm,
    Everytime I try and post the small complete program, the Forum deletes the post...

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,365
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: KeyListener

    The forum's software/server has problems occasionally. You now have 3 copies here

    How do you know if a key listener method is called? None of them call println to print out a message.

    Where is the GUI being shown that has the focus and the listener added to it?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: KeyListener

    Norm,
    As you should be able to see in the code, I tried to set the KeyListener to the Container. I have a breakpoint in the debugger in all three key routines and it never gets there. I have also tried to set the KeyListener to the Frame with the same results. The only place I can get it to work is by setting the keyListener to the JButton. I'd rather not do that as I want any keypress in the application to get trapped...

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,365
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: KeyListener

    Where is the GUI being shown that has the focus and the listener added to it?
    What does the constructor for MyClass do to make any GUI that you can see?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: KeyListener

    Norm,
    Not sure what you mean. MyClass creates a JButton adds it to the Frame Container and displays the Frame with the button on it. That much works just fine. And as I mentioned if I assign the KeyListener to the JButton instead of the Frame or Container, it functions correctly as well...

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,365
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: KeyListener

    MyClass creates a JButton adds it to the Frame
    Sorry, I do NOT see a constructor for the MyClass class.
    Are you talking about what is done in the main() method?


    Suggestion: move all the GUI creation code from the main() method to a constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: KeyListener

    @Norm: his posts with code were "moderated" and thus only showing to us moderators. To show them, we must approve them with Moderator Tools. I've done this with one of his posts so that it now shows to all.

  11. The Following User Says Thank You to curmudgeon For This Useful Post:

    Norm (January 25th, 2013)

Similar Threads

  1. implementing KeyListener
    By FARGORE in forum AWT / Java Swing
    Replies: 1
    Last Post: October 18th, 2012, 08:32 AM
  2. keyListener not working
    By soradogoof in forum Java Applets
    Replies: 2
    Last Post: August 14th, 2012, 05:33 PM
  3. Error on KeyListener
    By Colino in forum AWT / Java Swing
    Replies: 12
    Last Post: January 14th, 2012, 02:58 AM
  4. help with KeyListener
    By all_pro in forum AWT / Java Swing
    Replies: 4
    Last Post: April 14th, 2011, 06:51 AM
  5. Problem with KeyListener
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 01:18 PM