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 9 of 9

Thread: KeyListener won't listen

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default KeyListener won't listen

    Why won't my "player1" move???
    Class with main
    import java.awt.Color;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
     
    public class Spelplan extends JPanel implements KeyListener
    {
     
        Gubbe player1;
     
        int x = 100;
        int y = 100;
     
        public Spelplan()
        {
            setLayout(null);
            player1 = new Gubbe(30, 50, x, y);    
            this.setLocation(20, 30);
            add(player1);
            player1.addKeyListener(this);
            this.setBackground(Color.black);
     
        }
     
        public static void main(String[] args) 
        {
            JFrame f = new JFrame();
            f.setSize(300,300);
            f.setLocation(200,200);
            f.setTitle("RogueTest");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Spelplan p = new Spelplan();
            f.add(p);
            f.setVisible(true);
     
        }
     
        public void keyTyped(KeyEvent e)
        {
     
        }
     
        public void keyPressed(KeyEvent e)
        {
     
            if(e.getKeyCode() == 38)
            {
                System.out.println("Pressed");
                y = y + 10;
            }
     
            repaint();
        }
     
        public void keyReleased(KeyEvent e)
        {
     
        }
     
    }//class

    Other class
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import javax.swing.JPanel;
     
     
    public class Gubbe extends JPanel
    {
        private int bas, hojd, xpos, ypos;
     
     
     
        public Gubbe(int b, int h)
        {
         bas = b;
         hojd = h;
         this.setBackground(null);
         this.setPreferredSize(new Dimension(bas, hojd));
        }
     
     
     
        public Gubbe(int bas, int hojd, int xpos, int ypos)
        {
            this.bas = bas;
            this.hojd = hojd;
            this.xpos = xpos;
            this.ypos = ypos;
            this.setSize(bas, hojd);
            this.setLocation(xpos - bas/2, ypos);
     
     
        }
     
     
     
        @Override
        public void paintComponent(Graphics g)
        {
            int x[] = { bas/2, 0, bas};
            int y[] = { 0, hojd, hojd };
     
            g.setColor(Color.red);
            g.fillOval(10,20, 20, 20);
     
     
     
        }
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: KeyListener won't listen

    Does the panel with the listener have the focus?

    Move your debugging print statement outside of the if clause to see if the method is being called.

    What key is tested for here? Why not use the VK_... variable vs the 38?

    if(e.getKeyCode() == 38)
    Last edited by Norm; June 22nd, 2011 at 06:50 AM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: KeyListener won't listen

    Keylisteners require the component to have focus. An alternative would be to use KeyBindings: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: KeyListener won't listen

    I tried using the .setFocusable(true) in "public Spelplan". I also kept the printline "Pressed" inside the if-statement. Now it says "Pressed", but the component does not move.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: KeyListener won't listen

    but the component does not move.
    What will make it move? What statement is supposed to make it move?

  6. #6
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: KeyListener won't listen

    Quote Originally Posted by Norm View Post
    What will make it move? What statement is supposed to make it move?
    Because I change the value of y:
    y = y + 10;

    Or?? wouldn't that work?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: KeyListener won't listen

    What difference does changing the value of y make to where the component is?

  8. #8
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: KeyListener won't listen

    Quote Originally Posted by Norm View Post
    What difference does changing the value of y make to where the component is?
    Because the Y value is the components Y - value. When I change the Y value
    int y = 100;
    the component is changing it's position. Then I just thought it sounded reasonable that it would also change position if I made the value change by pressing the "up" - key. It did not do that

  9. #9
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: KeyListener won't listen

    Yeah, I made it work!

    if(e.getKeyCode() == 38)
            {
                y = player1.getY();
                System.out.println("Pressed");
    //            y = 200;
                player1.setLocation(x, y -= 10);
                repaint();
     
            }

Similar Threads

  1. help with KeyListener
    By all_pro in forum AWT / Java Swing
    Replies: 4
    Last Post: April 14th, 2011, 06:51 AM
  2. Can't listen on ports < 1024 in linux
    By burek in forum Java Networking
    Replies: 2
    Last Post: December 19th, 2010, 05:48 AM
  3. [SOLVED] Listen to two JRadioButton + ons JButton to produce output
    By voltaire in forum AWT / Java Swing
    Replies: 1
    Last Post: May 12th, 2010, 03:46 PM
  4. My KeyListener is not Working!!
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2010, 05:18 PM
  5. Problem with KeyListener
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 01:18 PM