1. Joined
    06 Mar '12
    Moves
    642
    11 Feb '20 20:444 edits
    Any java experts here?

    I tried to for SEVERAL HOURS to post this question on ANY java forum but each time I just couldn't get past the registration for the forum and gave up in despair but now decided to try my luck here despite this being not a java forum;

    When I run this program, why is this keyReleased method never called when I press a key?

    I cannot ever seem to get any Java program to allow user keyboard input via keyReleased method call.
    When I run this program, when I press keys on my keyboard I find nothing is printed in System.out
    thus indicating the keyReleased method is never called!

    Any help would be greatly appreciated.


    import javax.swing.*;
    import java.awt.Color;
    import java.awt.event.*;

    public class Testing extends JPanel{

    public static void main(String[] args) {

    Testing panel = new Testing();
    panel.setBackground(Color.WHITE);

    JFrame frame = new JFrame("Testing" );
    frame.add(panel);
    frame.setBounds(40, 60, 300, 300);
    frame.setVisible(true);
    frame.setFocusable(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel.addKeyListener(new Key_listener());
    panel.setVisible(true);
    panel.setFocusable(true); //-so the key listener can detect and respond to key release.

    }// end of main method.

    static class Key_listener extends KeyAdapter{

    @Override
    public void keyReleased(KeyEvent e) {
    System.out.println(" keyReleased on char: " + e.getKeyChar() );
    }// end of keyReleased method.

    } // end of Key_listener inner-class.

    } // end of Testing class.
  2. Joined
    06 Mar '12
    Moves
    642
    11 Feb '20 22:25
    Although I STILL haven't worked out what is wrong with that program, I have just worked out a very complicated but reasonable workaround the problem in the much more complex program I am writing thus I am now not so desperate to know what is wrong with it.
    But I would STILL like to know what is wrong with that above program!
  3. Subscribersonhouse
    Fast and Curious
    slatington, pa, usa
    Joined
    28 Dec '04
    Moves
    53223
    11 Feb '20 22:30
    @humy
    Good luck. WAY above MY pay grade😉
  4. Joined
    16 Feb '08
    Moves
    116779
    12 Feb '20 11:44
    @humy said
    Any java experts here?

    I tried to for SEVERAL HOURS to post this question on ANY java forum but each time I just couldn't get past the registration for the forum and gave up in despair but now decided to try my luck here despite this being not a java forum;

    When I run this program, why is this keyReleased method never called when I press a key?

    I cannot ever seem to ge ...[text shortened]... end of keyReleased method.

    } // end of Key_listener inner-class.

    } // end of Testing class.
    Have you tried switching it off and on again?
  5. Joined
    06 Mar '12
    Moves
    642
    12 Feb '20 16:10
    @divegeester said
    Have you tried switching it off and on again?
    If what you mean is close down the java builder and then restart, yes.
  6. Joined
    06 Mar '12
    Moves
    642
    14 Feb '20 14:017 edits
    After reading over the net about how to export the program, I finally worked out what I was doing wrong!
    When exporting it to my desktop as a standalone program, I kept selecting the "JAR file" option when I should select the "Runnable Jar file" option.
    I now learned that "JAR file" option is ONLY for when it is an Aplet program, which the above isn't, while the "Runnable Jar file" option is for when it is a NON-applet program and thus with main(String[] s) method, like my program above.

    I now see that it DOES open when I click it on my desktop;
    BUT only to now discover yet ANOTHER NEW problem!
    This time, although it opens and runs, the panel that appears is MASSIVE and is appears not where I specified it to be in the program but rather much lower down and to the right on my computer screen and with much of it off the screen and hidden from view and any words printed on that panel by the program (the code for that isn't shown in my above OP program) are MASSIVE!
    And yet when I open that exact same program through eclipse, it all appears normal size and correctly positioned!
    Any theories why?
  7. R
    Standard memberRemoved
    Joined
    09 Sep '18
    Moves
    20590
    14 Feb '20 14:03
    @humy said
    After reading over the net about how to export the program, I finally worked out what I was doing wrong!
    When exporting it to my desktop as a standalone program, I kept selecting the "JAR file" option when I should select the "Runnable Jar file" option.
    I now learned that "JAR file" option is ONLY for when it is an Aplet program, which the above isn't, while the "Runnable Jar ...[text shortened]... IVE!
    -and yet when I open it through eclipse it appears normal size and correct!
    Any theories why?
    Gremlins
  8. SubscriberVery Rusty
    Treat Everyone Equal
    Halifax, Nova Scotia
    Joined
    04 Oct '06
    Moves
    598094
    14 Feb '20 22:461 edit
    @humy said
    After reading over the net about how to export the program, I finally worked out what I was doing wrong!
    When exporting it to my desktop as a standalone program, I kept selecting the "JAR file" option when I should select the "Runnable Jar file" option.
    I now learned that "JAR file" option is ONLY for when it is an Aplet program, which the above isn't, while the "Runnable Jar ...[text shortened]... gram through eclipse, it all appears normal size and correctly positioned!
    Any theories why?
    Trump or the Russians could be involved?

    Seriously check the Technology Forum.

    -VR
Back to Top

Cookies help us deliver our Services. By using our Services or clicking I agree, you agree to our use of cookies. Learn More.I Agree