Results 1 to 5 of 5
  1. #1
    VirusParadox is offline Member
    Join Date
    Dec 2013
    Posts
    7
    Rep Power
    0

    Question Why won't this KeyListener work?!

    I made a KeyListener in java (or tried to) but it's not giving any output. I don't know what's wrong with it. Here's the code:
    Java Code:
    package main;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class KeyListenerTest implements KeyListener {
    	public void keyPressed(KeyEvent arg0) {
    		// TODO Auto-generated method stub
    		System.out.println("You pressed " + arg0.getKeyChar() + ".");
    	}
    
    	public void keyReleased(KeyEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	public void keyTyped(KeyEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    	public static void main(String[] args){
    		JFrame frame = new JFrame("Test You-Know-What");
    		JPanel panel = new JPanel();
    		JTextArea text = new JTextArea();
    		JScrollPane scroll = new JScrollPane(text);
    		GridLayout gl = new GridLayout(1,1);
    		panel.setLayout(gl);
    		panel.add(scroll);
    		frame.setContentPane(panel);
    		frame.setSize(500,500);
    		frame.setVisible(true);
    	}
    	
    
    }
    It doesn't show anything in the console, I don't know what I did wrong.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Why won't this KeyListener work?!

    First you need to add your key listener to you JTextArea via the addKeyListener method. You may also want to set your panel size to a preferredSize and not your JFrame. Then to a frame.pack() before you setVisible(true);

    Regards,
    Jim
    Last edited by jim829; 12-18-2013 at 02:18 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    VirusParadox is offline Member
    Join Date
    Dec 2013
    Posts
    7
    Rep Power
    0

    Question Re: Why won't this KeyListener work?!

    Thanks, I wondered about that. I still don't understand what I should put int the parenthesis for addKeyListener(). Also, could I put a keylistener on a jpanel, so nothing has to be typed?

    Thanks,
    Virus

  4. #4
    VirusParadox is offline Member
    Join Date
    Dec 2013
    Posts
    7
    Rep Power
    0

    Default Re: Why won't this KeyListener work?!

    Never mind, I got it. I just did the same as action listeners

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,610
    Rep Power
    5

    Default Re: Why won't this KeyListener work?!

    In the addKeyListener method as in any other add listener method you put in an instance of the class that implements the listener. In your case, it would be
    Java Code:
    text.addKeyListener(new KeyListenerTest());
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. KeyListener
    By tim8w in forum AWT / Swing
    Replies: 1
    Last Post: 01-28-2013, 07:57 PM
  2. Help with KeyListener
    By armyson in forum New To Java
    Replies: 1
    Last Post: 11-25-2011, 12:56 PM
  3. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 01:45 PM
  4. KeyListener doesnt work some times.
    By Addez in forum New To Java
    Replies: 15
    Last Post: 09-21-2010, 04:25 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
  •