KeyListener Only Sometimes Works - java

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

Related

Block Swing pop-up from losing focus and closing

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.

KeyListener not working using Applet

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 ;)

Key event not captured in Applet AFTER I've pressed one of my GUI buttons

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.

Algorithm for invalidation / refresh system in Guis

I'm making a Gui for games, and I only want the Gui to redraw a widget when it is necessary. Is there an algorithm to knowing when the widget needs to be invalidated? otherwise it seems very error prone.
Thanks
How does Windows's GUI do it.
Is there an algorithm to knowing when the widget needs to be invalidated?
Generally when you change a property of your widget.
If you look at the standard Swing components they always repaint when methods like setFont(), setBackground(), setText(), setLocation(), setSize() ... are invoked.
I have no idea what your widget does buy you should follow the same concept, that is is you change a property that affects the painted stated of the widget then revalidate() and repaint() it.
it's not really an algorithm at the level of your question.
In the OO world, you will make an object out of the 'widget'
then that 'widget' will be aware of where it is in the world.
then you can check whether any other widget overlaps with it for redraw - or if it has moved etc.

call transferFocus or requestFocus?

in my application i get a component to focus ,
it could be a jpanel , and is could be a jbutton or a user custom made component
how can i know when to call transferFosus ,and when to call requestFocus
thanks you
transferFocus() sends focus to the next component. Also note that
transferFocus() will always transfer the focus in the forward direction.
requestFocus() sends focus to calling component. However, there is no guarantee that this will be successful. Focus behavior is platform-dependent to certain extend.
The recommended mentod for gaining focus is to use requestFocusInWindow(). Refer to this post - might come very handy in playing with focus.
Use transferFocus() when you want to advance focus according to the focus order.
requestFocus() is used to explicitly set the focus to a component.
Some background reading in Focus on Swing
It's rare that you would need to call either since its usually appropriate to let the user's keyboard/mouse actions determine focus. But transferFocus send focus away from your component and requestFocus brings focus to your component.

Categories

Resources