CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    Unhappy key handler problems

    Here is my code:
    Code:
     =jtextfield1.addKeyListener(new KeyAdapter(){
                      @Override
                      public void keyPressed(KeyEvent e)
                      {
                          if (e.getKeyCode()==KeyEvent.VK_ENTER)
                          {   jtextfield1.getText();
                            JOptionPane.showMessageDialog(null,null,"Success",JOptionPane.INFORMATION_MESSAGE);
                         }
    )};
    }
    I am getting these errors

    java.lang.ClassFormatError: Invalid superclass index 0 in class file MycologyFrame
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
    Error: A JNI error has occurred, please check your installation and try again

    I have the proper imports in there too. I basically followed a youtube on it and mine isn't working . I don't know why ? Help
    Last edited by Azoth; April 13th, 2017 at 10:55 PM.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: key handler problems

    Why do you think the error is related to the posted code?
    How are you using JNI?
    Norm

  3. #3
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    Re: key handler problems

    Quote Originally Posted by Norm View Post
    Why do you think the error is related to the posted code?
    How are you using JNI?
    When I remove the key handler my program run just fine.

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: key handler problems

    I have no idea why that code would cause an exception.
    What happens if the JOptionPane statement is removed?

    What is the = that is shown in the posted code:
    =jtextfield1.addKeyListener(new KeyAdapter(){
    Norm

  5. #5
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    Re: key handler problems

    Quote Originally Posted by Norm View Post
    I have no idea why that code would cause an exception.
    What happens if the JOptionPane statement is removed?

    What is the = that is shown in the posted code:
    =jtextfield1.addKeyListener(new KeyAdapter(){
    Typo

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: key handler problems

    Does the code compile without errors?
    Are you trying to run the code with compiler errors?
    Norm

  7. #7
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    Re: key handler problems

    Quote Originally Posted by Norm View Post
    Does the code compile without errors?
    Are you trying to run the code with compiler errors?
    I removed the Popup window and put in a System.out.println("success") but
    upon running it printed nothing.

    I tried something like this
    Class appname extends jframe implements keyHandler{
    I get the error class appname not abstract ?
    Last edited by Azoth; April 14th, 2017 at 04:14 PM.

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: key handler problems

    it printed nothing.
    That means the code was not executed.
    Is the keyPressed method called? Add a print statement to show if it was called and what the value of the passed key code was.
    Norm

  9. #9
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    Re: key handler problems

    Quote Originally Posted by Norm View Post
    That means the code was not executed.
    Is the keyPressed method called? Add a print statement to show if it was called and what the value of the passed key code was.
    Pressing enter should call it. How do do I call it?

  10. #10
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: key handler problems

    Can you make a small, simple program for testing that shows the problem?
    Norm

  11. #11
    Join Date
    Feb 2017
    Location
    Montreal
    Posts
    54

    Re: key handler problems

    Problem solved.i had to change keyevent e to java.awt.event.KeyEvent in the parameter. It then recognized the
    getKeyCode() method. Before it stated it could not find the symbol.

  12. #12
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: key handler problems

    it could not find the symbol.
    When you get compiler errors, you MUST fix them before trying to execute the code.

    It's a waste of time posting short pieces of code without also posting the error messages it has.
    Norm

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured