I have an applet using a keylistener, but the events are not triggered when I press the keys..
I also add the setFocusable(true), but didn't work too..
Here is my code
http://pastebin.com/WnFVVps7
I am creating a new KeyListener and adding it to the applet object.
Also I am using setFocusable(true) on my init() method.
I don't know what to do more, can someone take a look at my code? thanks.
Thanks alot in advance ;)
This is common issue with KeyListener.
The problem is, KeyListener will only respond to key strokes when the component it is registered to is focusable AND has keyboard focus. This, generally, makes it inappropriate for general use, it's really a specialised listener.
Instead, you should use the Key Bindings API, which allows you to control the focus level that the key strokes will be triggered at
If you want to catch global key events for the application instead of just for a component, use KeyEventPostProcessor. This should be used carefully.
I have not tried it, but maybe the problem is that you declare a class KeyListener and also import another class by that name. Try renaming your KeyListener-class to something less generic.
Also: Are you sure the class KeyListener should implement KeyAdapter and not java.awt.event.KeyListener?
Do KeyListener.keyReleased() and .keyTyped() work?
There was a while(true) with Thread sleep inside it. The keystrokes were not triggered because of that, I changed the code to start the while(true) inside a new Thread and works now :)
Thanks all for your patience ;)
Related
I'm developing a plugin for IntelliJ IDEA, which obviously uses Swing.
For a feature I have introduced I'd like to stop a JPopupMenu which uses JCheckBoxMenuItems from losing focus and closing.
You can see it in action.
I've debugged the code, but I couldn't figure out how to do it, also being I'm not that into Swing.
Could you maybe point me to useful listeners/blocks of code/ways to prevent this?
If you want to see code, the IntelliJ classes are
ActionPopupMenuImpl.MyMenu
ActionMenuItem
Edit: a better way need to be found as the uiRefreshed event isn't always called at the right time.
Just coordinate your code in a good way ;)
The Swing mechanism in IDEA is too complicated, and maybe it's better to not touch it. Just know that the mouse events are handled by a special listener and then redirected to Component(s).
That said, having an hold on the menu ActionButton. You can listen for the Lookup's uiRefreshed event and programmatically:
myMenuButton.click()
That's all.
You need to call it after the UI has been refreshed because the LookupUi might have changed in dimension or location.
Learning GUIs for an assignment, and would appreciate some advice...no code, for the aforementioned reason.
I have a programme with a JFrame, and a single JPanel, that contains methods that take keyboard input using the KeyEvent class.
My class that extends JPanel, has the following in it:
setFocusable(true);
requestFocus();
I've since modified the programme, to include a second JPanel (to add a control panel to the right containing 4 JButtons).
The problem is that the keyboard input no longer works on the first JPanel when I run the programme (it did before).
The keyboard input on the right JPanel only works before the second JPanel has been added to the programme. If I remove the second JPanel, keyboard input works, when it is there it doesn't....
I realised that I had setFocusable(true); and requestFocus(); in both panels, so I deleted it from the second panel (with the JButtons), but it still doesn't work. I sense it's a focus issue.....any advice?
Do I need to look at KeyBindings (don't know what this is yet, but a few similar threads suggest it)....
We can't tell you what is wrong based on two lines of code. We have no idea what panel1 and panel2 do or what components they contain.
My class that extends JPanel, has the following in it:
A couple of problems with that:
You should NOT be using the requestFocus() method. Read the API for that method and it will tell you the appropriate method to use.
Even if you do use the appropriate method, you can't use that method in the constructor of a class. Requesting focus on a component can only be done to visible components on a GUI.
Do I need to look at KeyBindings
Yes, Swing has newer and better API's than AWT. In AWT you didn't have a choice. In Swing you should be using Key Bindings. All Swing components do use Key Bindings and Actions. One of the main reasons is you don't have the focus issue.
Start by reading the Swing tutorial for Swing basics. There are sections on:
How to Use Key Bindings
How to Use Actions to get you started.
A Key Binding is simply the process of mapping a KeyStroke to an Action.
Also, in the future, when you ask a question post a proper SSCCE that demonstrates the problem.
If you mean with "pass back and force" that the focus should move on tab keypress between those two, use a FocusTraversalPolicy where you define who should get focus in which order.
When you click on a button, that button receives the keyboard focus. If you want the focus to revert back to the first JPanel, then you should add an ActionListener to each button, and in the actionPerformed() method of the listener just add the line:
firstPanel.requestFocusINnWindow();
I am making a sort of remote control program for Lego EV3 robot. that part is irrelevant. So i made a GUI and i want to control the robot when i press keys. I understand that i have to use something called KeyListener and i even saw a tutorial which is supposed to work.
The GUI class code is right here. Kinda long but it has KeyPressed event at the end.
http://pastebin.com/QK639BDs
I am not sure what i am doing wrong but the program doesn't detect if any key is pressed at all. Any.
I would really appreciate any help on how to make that work.
EDIT:
keyManager=KeyboardFocusManager.getCurrentKeyboardFocusManager();
keyManager.addKeyEventDispatcher(new KeyEventDispatcher() {
// UP:38 DOWN:40 LEFT:37 RIGHT:39
public boolean dispatchKeyEvent(KeyEvent e) {
if(e.getID()==KeyEvent.KEY_PRESSED && e.getKeyCode()==38){
System.out.println("UP");
return true;
}
if(e.getID()==KeyEvent.KEY_RELEASED&& e.getKeyCode()==38){
System.out.println("RELEASED");
return true;
}
return false;
}
});
So i browsed around and i found KeyboardFocusManger which is sort of working for me. I am testing it with a println. I am having only one problem. While i hold down the UP key i want it to only print UP once. Because the UP key will basically start the motor and it will keep moving until the key release will stop it.
Any ideas on how to do that?
KeyListener has issues, particularly with focus, so when any of your text fields are focused, your KeyListener won't respond, for example.
A better solution is to make use of the key bindings API, which allows you to control the level of focus required in order to trigger the key event. In combination with the Action API, you can define common actions for both your keys and buttons, for example.
Take a look at How to Use Key Bindings and How to Use Actions for more details.
Ps- I'm jealous and I wish you luck ;)
Do this:
frame.getContentPane().addKeyListener(this);
You may have to do it with different components depending on which one you want to have a key listener on.
I have music Applet which uses keyboard, mouse and GUI buttons. When applet is first loaded keyboard events work fine, as do mouse events. However, after I have pressed one of my GUI buttons the mouse events still work but the keyboard events don't, and don't start working again until I refresh the applet.
After hunting around on the net I found some posible solutions, I've tried adding button.setFocusable(true); and button.addKeyListener(this); to all my buttons, and and my panels. No effect at all. I've seen recommendations for converting to a JApplet and using key binding, but surely there must be a simpler way?
Sorry for the lack of code, I've been working on this project since I was a newbie and it's a bit of a mess, and very long!
Any help much appreciated!
button.setFocusable(true); and button.addKeyListener(this); to all my buttons
For JButton use Swing Action or default implementations for ActionListener, rather than KeyBindings (for Swing based Container and JComponents), nor using KeyListener
EDIT
if isn't there really important reasons, don't use prehistoric AWT Applet, use JApplet, may be sufficient would be plain JFrame
Try to cut the problem area out of your project and put it here. Its highly probable, than while localizing the problem area you'll find some errors already.
If your project is a mess already, then the first and the most important thing you should do, is to order it. If it is a mess for you, that means you don't understand it. So, it simply can't work. That is your first and main error.
I have made a Pong clone, and I've come across a problem. The KeyListener only works sometimes. There is no pattern that I have been able to find.
For reference here is my Pong.java(the main class): http://pastebin.com/8d7BqK4x
Here is the Board.java(Graphics and KeyListener): http://pastebin.com/0zb526BE
KeyEvents are only generated when a component has focus.
A better approach is to use Key Bindings which work even when the component doesn't have focus.
Usually you don't want to use a KeyListener due to focus issues (which is likely why your listener only listens at times -- likely your listen-to component loses focus and so the KeyListener loses its function.
Better to use Key Bindings where you don't have to worry so much about focus issues and where you're dealing with a higher level construct, one that Swing uses itself to listen to components. The tutorials will "show you the way".