Results 1 to 4 of 4
  1. #1
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default KeyListener not working

    How do I make KeyListener work? I know for sure the only bugs in my code have to do with KeyListener:
    Risk.java:
    Java Code:
    import java.awt.Toolkit;
    import javax.swing.*;
    import java.awt.*;
    import java.text.NumberFormat;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.*;
    
       public class Risk extends JPanel {
       public int leftNumber, rightNumber, setbad;
       public JButton Enter;
       public JFormattedTextField stupid, yummy;
       public JLabel smart, dumb, nook, shook;
       public String nos, yos, setnice;
       public NumberFormat yucker = NumberFormat.getNumberInstance();
       public NumberFormat nucker = NumberFormat.getNumberInstance();
    
       public Risk() {
          setbad = 1;
          smart = new JLabel("First Number:");
          add(smart);
    	      stupid = new JFormattedTextField(yucker);
          stupid.setColumns(3);
          stupid.setText("0");
          add(stupid);
          boolean isDigit;
          dumb = new JLabel("Second Number:");
          add(dumb);
          yummy = new JFormattedTextField(nucker);
          yummy.setColumns(3);
          yummy.setText("0");
          add(yummy);
          Enter = new JButton("Enter");
          add(Enter);
          Enter.addActionListener(new ButtonListener());	
    	
    	
       }
    public void keyPressed(KeyEvent e)
    {
    System.out.println("pressed");
    }
       class ButtonListener implements ActionListener {
          ButtonListener() {
          }
    
          public final void actionPerformed(ActionEvent e) {
             if (e.getActionCommand().equals("Enter")) {
    		if (setbad == 1) {
    			shook = new JLabel("0");
    			nook = new JLabel("0");
    			add(shook);
    			add(nook);
    				
              	String nos = stupid.getText();
    	  	String yos = yummy.getText();
    	   	shook.setText("First Editable Number: " + nos);
    	   	nook.setText("Second Editable Number: " + yos);
    		setbad ++;
    	revalidate();
    }
    else {
    	  String nos = stupid.getText();
    	  String yos = yummy.getText();
    	   shook.setText("First Editable Number: " + nos);
    	   nook.setText("Second Editable Number: " + yos);
    	revalidate();
    	}
    	
    	
    	  
             }
          }
       }
    }
    RiskApp.java:
    Java Code:
    import javax.swing.*;
    import java.text.NumberFormat;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.SwingUtilities;
    import java.awt.*;
    import java.awt.event.*;
    
    public class RiskApp extends JFrame implements KeyListener{
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("Risk");
    JPanel panel = new JPanel(new BorderLayout());
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(1,2));
    addKeyListener(this);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Risk riskPanel = new Risk();
            frame.getContentPane().add(riskPanel);
            frame.setSize(305,90);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
    try {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (UnsupportedLookAndFeelException e) {
    } catch (ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    }
                    createAndShowGUI();
                }
            });
        }
    }
    Error Message:
    Java Code:
    C:\Users\cc11rocks\Documents\JavaCrap>javac RiskApp.java
    RiskApp.java:9: RiskApp is not abstract and does not override abstract method ke
    yReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListener
    public class RiskApp extends JFrame implements KeyListener{
           ^
    RiskApp.java:20: non-static variable this cannot be referenced from a static con
    text
    addKeyListener(this);
                   ^
    RiskApp.java:20: non-static method addKeyListener(java.awt.event.KeyListener) ca
    nnot be referenced from a static context
    addKeyListener(this);
    ^
    3 errors
    Please help me as this is my first time trying to use KeyListeners. Risk.java compiles fine.
    Thanks,
    cc11rocks
    Last edited by cc11rocks; 02-08-2011 at 03:46 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If your class implements any interface it must implement all the methods of that interface. You have Risk implementing a KeyListener but you give it none of the methods of KeyListener.

    Having said that, don't have your GUI implement listeners, including KeyListener and ActionListener. You're far better off using anonymous inner classes (if you don't understand what these are, look them up). Also, on an unrelated note you're often better off using key bindings not key listeners.

    Finally, "this" has no meaning inside of a static class. But again, this wouldn't be an issue if you used anonymous inner classes.

  3. #3
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

    Default Errors!

    Risk.java
    Java Code:
    import java.awt.Toolkit;
    import javax.swing.*;
    import java.awt.*;
    import java.text.NumberFormat;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.*;
    import javax.swing.SwingUtilities;
    
       public class Risk extends JPanel {
       public int leftNumber, rightNumber, setbad;
       public JButton Enter;
       public JFormattedTextField stupid, yummy;
       public JLabel smart, dumb, nook, shook;
       public String nos, yos, setnice;
       public NumberFormat yucker = NumberFormat.getNumberInstance();
       public NumberFormat nucker = NumberFormat.getNumberInstance();
    
       public Risk() {
          setbad = 1;
          smart = new JLabel("First Number:");
          add(smart);
    	      stupid = new JFormattedTextField(yucker);
          stupid.setColumns(3);
          stupid.setText("0");
          add(stupid);
          boolean isDigit;
          dumb = new JLabel("Second Number:");
          add(dumb);
          yummy = new JFormattedTextField(nucker);
          yummy.setColumns(3);
          yummy.setText("0");
          add(yummy);
          Enter = new JButton("Enter");
          // enter.addActionListener(new ActionListener());
          add(Enter);
          Enter.addActionListener(new ButtonListener());
    	
       class ButtonListener implements ActionListener {
          ButtonListener() {
          }
    
          public final void actionPerformed(ActionEvent e) {
             if (e.getActionCommand().equals("Enter")) {
    		if (setbad == 1) {
    			shook = new JLabel("0");
    			nook = new JLabel("0");
    			add(shook);
    			add(nook);
    				
              	String nos = stupid.getText();
    	  	String yos = yummy.getText();
    	   	shook.setText("First Editable Number: " + nos);
    	   	nook.setText("Second Editable Number: " + yos);
    		setbad ++;
     //Toolkit.getDefaultToolkit().beep(); 
    Enter.requestFocus();
    revalidate();
    }
    else {
    	  String nos = stupid.getText();
    	  String yos = yummy.getText();
    	   shook.setText("First Editable Number: " + nos);
    	   nook.setText("Second Editable Number: " + yos);
     //Toolkit.getDefaultToolkit().beep(); 
    Enter.requestFocus();
    revalidate();
    	}
    KeyListener listener = new KeyListener()
    {
    public void keyPressed(KeyEvent e) {
    if (listener.equals("a")) {
    System.out.println("It works!");
    };
    else {
    };
    	};
    };
    };
    };
    	
    	  
            
          };
       };
    };
    Error:
    Java Code:
    C:\Users\cc11rocks\Documents\JavaCrap>javac Risk.java
    Risk.java:38: cannot find symbol
    symbol  : class ButtonListener
    location: class Risk
          Enter.addActionListener(new ButtonListener());
                                      ^
    Risk.java:72: <anonymous Risk$1ButtonListener$1> is not abstract and does not ov
    erride abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.Ke
    yListener
    {
    ^
    2 errors
    
    C:\Users\cc11rocks\Documents\JavaCrap>javac RiskApp.java
    .\Risk.java:38: cannot find symbol
    symbol  : class ButtonListener
    location: class Risk
          Enter.addActionListener(new ButtonListener());
                                      ^
    .\Risk.java:72: <anonymous Risk$1ButtonListener$1> is not abstract and does not
    override abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.
    KeyListener
    {
    ^
    2 errors

  4. #4
    cc11rocks is offline Member
    Join Date
    Jan 2011
    Posts
    49
    Rep Power
    0

Similar Threads

  1. keylistener + applet isn't working
    By brandon95 in forum Java Applets
    Replies: 6
    Last Post: 01-03-2011, 02:10 PM
  2. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 04:14 PM
  3. keyListener isn't working for me
    By lost_in_java in forum AWT / Swing
    Replies: 7
    Last Post: 12-05-2008, 04:24 AM
  4. KeyListener Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:46 PM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

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