Go back
What is wrong with this (small) java program?

What is wrong with this (small) java program?

General

4 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.


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!


@humy
Good luck. WAY above MY pay grade😉



-Removed-
If what you mean is close down the java builder and then restart, yes.

7 edits
Vote Up
Vote Down

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?

Vote Up
Vote Down

@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

1 edit
Vote Up
Vote Down

@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