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

    Default Not abstract and doesn't override abstract method keyReleased

    Test.java:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     //import java.awt.event.KeyListener;
    	public class Test extends JPanel implements KeyListener, ActionListener {
    	public String Text, Input, Jacob;
    	public JTextField User;
    	public JButton Enter; // Clear Text;
    	public TextArea moss, lol;
    	public JLabel Here, There;
    	public int Yojava;
    	public Test() {
    	Yojava = 1;
    	moss = new TextArea();
    	moss.setColumns(48);
    	moss.setRows(5);
    	moss.setEditable(false);
    	//int setLineWrap(true);
    	//moss.setFont(BLUE);
    	moss.setText("User 1 has entered the chatroom. Please enter your name.");
    	//moss.addKeyListener(this);
    	add(moss);
    	Here = new JLabel("Your chat:");
    	add(Here);
    	User = new JTextField();
    	User.setText("Your name here");
    	User.setColumns(10);
    	add(User);
    	lol = new TextArea();
    	lol.setColumns(40);
    	lol.setRows(5);
    	//int setLineWrap(true);
    	//lol.setBackground(red);
    	add(lol);
    	Enter = new JButton("Enter");
    	add(Enter);
    	Enter.addActionListener(new ButtonListener());
    }
     public void keyPressed(KeyEvent e) {
    	System.out.println("It works");
     }
    class ButtonListener implements ActionListener {
    ButtonListener() {
    }
    public final void actionPerformed(ActionEvent e) {
    	if (e.getActionCommand().equals("Enter")) {
    	if (Yojava == 1) {
    	if (Jacob == ("Your name here")) {
    	moss.append("\nPlease enter your name.");
    	}
    	String Input = lol.getText();
    	String Jacob = User.getText();
    	moss.append("\n" + Jacob + ": " + Input);
    	lol.setText("");
    	Yojava ++;
    	revalidate();
    	}
    	else {
    	String Input = lol.getText();
    	String Jacob = User.getText();
    	moss.append("\n" + Jacob + ": " + Input);
    	lol.setText("");
    	revalidate();
    	if (Jacob == "Your name here") {
    	moss.append("\nPlease enter your name.");
    	}
    }
    }
    }
    }
    }
    TestApp.java:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TestApp extends JFrame {
    	private static void createAndShowGUI() {
    	JFrame frame = new JFrame("Test");
    	//TextArea lol;
    	JPanel panel = new JPanel(new BorderLayout());
    	Test testPanel = new Test();
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.getContentPane().add(testPanel);
    	frame.setSize(330, 300);
    	frame.setResizable(false);
    	
    	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();
                }
            });
        }
    }
    I'm receiving an error:
    Java Code:
    C:\Users\cc11rocks\Documents\JavaCrap>javac Test.java
    Test.java:5: Test is not abstract and does not override abstract method keyRelea
    sed(java.awt.event.KeyEvent) in java.awt.event.KeyListener
            public class Test extends JPanel implements KeyListener, ActionListener
    {
                   ^
    1 error
    
    C:\Users\cc11rocks\Documents\JavaCrap>javac TestApp.java
    .\Test.java:5: Test is not abstract and does not override abstract method keyRel
    eased(java.awt.event.KeyEvent) in java.awt.event.KeyListener
            public class Test extends JPanel implements KeyListener, ActionListener
    {
                   ^
    1 error
    The reason for "javac" ing both is to show you the problem is with Test.java, specifically the line:
    Java Code:
    public class Test extends JPanel implements KeyListener, ActionListener {
    What's is wrong and how can I fix it?
    Thanks,
    cc11rocks

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

    Default

    If a class implements an interface, it must implement all the methods of the interface. It looks like your error message is telling you that you're not doing this, that you're not implementing all the methods of the KeyListener.

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

    Default

    Used same code from:
    http://download.oracle.com/javase/tu...EventDemo.java
    Copied and pasted from KeyEventDemo.java:
    Java Code:
    public class KeyEventDemo extends JFrame
            implements KeyListener,
            ActionListener

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

    Default

    Quote Originally Posted by cc11rocks View Post
    Used same code from:
    http://download.oracle.com/javase/tu...EventDemo.java
    Copied and pasted from KeyEventDemo.java:
    Java Code:
    public class KeyEventDemo extends JFrame
            implements KeyListener,
            ActionListener
    Regardless of what you've copied and pasted, your class implements KeyListener and thus requires all the methods of KeyListener. The error is telling you specifically that you don't have a valid keyReleased method, and searching through your code confirms this. What more information do you need to convince you that the error message is steering you in the right direction?

    edit: and in fact the code you've linked to does have a valid keyReleased method, but yours doesn't. The error message isn't really that cryptic and tells you exactly what is wrong.

    Luck.
    Last edited by Fubarable; 02-19-2011 at 05:23 AM.

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

    Default

    Thank you so much for your help...Working code:
    Test.java:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.event.KeyListener.*;
    	public class Test extends JPanel implements KeyListener, ActionListener {
    	public String Text, Input, Jacob;
    	public JTextField User;
    	public JButton Enter; // Clear Text;
    	public TextArea moss, lol;
    	public JLabel Here, There;
    	public int Yojava;
    	public Test() {
    	Yojava = 1;
    	moss = new TextArea();
    	moss.setColumns(48);
    	moss.setRows(5);
    	moss.setEditable(false);
    	//int setLineWrap(true);
    	//moss.setFont(BLUE);
    	moss.setText("User 1 has entered the chatroom. Please enter your name.");
    	add(moss);
    	moss.addKeyListener(this);
    	Here = new JLabel("Your chat:");
    	add(Here);
    	User = new JTextField();
    	User.setText("Your name here");
    	User.setColumns(10);
    	add(User);
    	lol = new TextArea();
    	lol.setColumns(40);
    	lol.setRows(5);
    	//int setLineWrap(true);
    	//lol.setBackground(red);
    	add(lol);
    	Enter = new JButton("Enter");
    	add(Enter);
    	Enter.addActionListener(new ButtonListener());
    }
     public void keyPressed(KeyEvent e) {
    	System.out.println("It works");
     }
    public void keyReleased(KeyEvent e) {
    	System.out.println("It work");
    }
    public void keyTyped(KeyEvent e) {
    	System.out.println("It");
    }
    public void actionPerformed(ActionEvent e) {
    	System.out.println("Something is wrong?");
    }
    class ButtonListener implements ActionListener {
    ButtonListener() {
    }
    public final void actionPerformed(ActionEvent e) {
    	if (e.getActionCommand().equals("Enter")) {
    	if (Yojava == 1) {
    	if (Jacob == ("Your name here")) {
    	moss.append("\nPlease enter your name.");
    	}
    	String Input = lol.getText();
    	String Jacob = User.getText();
    	moss.append("\n" + Jacob + ": " + Input);
    	lol.setText("");
    	Yojava ++;
    	revalidate();
    	}
    	else {
    	String Input = lol.getText();
    	String Jacob = User.getText();
    	moss.append("\n" + Jacob + ": " + Input);
    	lol.setText("");
    	revalidate();
    	if (Jacob == "Your name here") {
    	moss.append("\nPlease enter your name.");
    	}
    }
    }
    }
    }
    }
    TestApp.java:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TestApp extends JFrame {
    	private static void createAndShowGUI() {
    	JFrame frame = new JFrame("Test");
    	//TextArea lol;
    	JPanel panel = new JPanel(new BorderLayout());
    	Test testPanel = new Test();
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.getContentPane().add(testPanel);
    	frame.setSize(330, 300);
    	frame.setResizable(false);
    	
    	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();
                }
            });
        }
    }
    Thank you so much. You are a lot of help Furable.
    Thanks,
    cc11rocks
    Last edited by cc11rocks; 02-19-2011 at 05:58 AM.

Similar Threads

  1. Replies: 3
    Last Post: 08-13-2010, 05:57 AM
  2. Replies: 2
    Last Post: 03-26-2010, 06:12 PM
  3. Replies: 3
    Last Post: 09-16-2009, 10:27 PM
  4. Replies: 2
    Last Post: 11-21-2008, 01:20 AM
  5. Replies: 6
    Last Post: 10-27-2008, 01:25 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
  •