Disable JButtons to read events from the keyboard - java

I have two JPanels. In the first one I have 3 JButtons and the second one is to draw images based on events read from the keyboard. If I set the JButtons with setEnabled(false); I can use the keyboard events as I expect (If I press up arrow the image moves up), but when the buttons are enabled nothing happens with the image. Even, if I press the space bar it behaves like if I clicked the button.

The problem is not with the JButtons, but is likely because your using a KeyListener. Don't use KeyListeners with Swing GUI's if you can avoid it and instead use Key Bindings. KeyListeners only work if the component being listened to has the focus, and when you have JButtons present, they will take the focus and prevent your KeyListener from working. Key Bindings, if done right, can avoid this problem.
For example, please see my code example here.

Related

Have KeyListener listen to different JFrame

Okay so long story short, here is what is happening and what I am trying to do.
I have a class called GameGUI, this is a JFrame.
This JFrame is populated with tiles(JLabels with ImageIcons)
I use my arrow keys to move my guy around the JFrame (just updates the JLabel Images)
I made a settings option, this is a new JFrame that I setVisbile(true), make changes, then setVisible(false)
After I setVisible(false) my arrow keys no longer make my character move on GameGUI.
I have tried the following, oh which none work: (all guesses based off Googling my problem)
GameGUI gg = new GameGUI();
gg.setFocusable(true);
gg.addKeyListener(null);
gg.requestFocusInWindow();
I cannot seem to find a way to get my KeyListener to move back to the GameGUI after I open (make visible) this settings menu then close it (make invisible). I do have radio buttons within the settings menu which is why I believe it gains focus, due to a physical mouse click.
Any help on getting focus back onto GameGUI would be much appreciated!
Thanks!
5.After I setVisible(false) my arrow keys no longer make my character move on GameGUI.
Don't use a KeyListener. Swing was designed to be used with Key Bindings.
See Motion Using the Keyboard, which will explain your probable problem and give the solution using Key Bindings.

Focusing in different JPanels? (java)

Quick question, I have 3 JPanels, one has a keylistener and it draw my game graphics, and you can alter it by moving your character around, etc. and I have a second JPanel which is just a JTextArea, and it just displays text, and finally I have another JPanel which has a JTextArea and a JButton, the text area takes inputs and you may hit enter key or click the button to send the text which will appear in the other text box. But my question is, how can I request focus in each panel? How do i transfer the focus with lets say a mouse click?
I know that if I dont constantly call requestFocus(true); for the first JPanel with displays all the graphics it wont work for some reason. I believe its because its always painting and maybe it looses focus on every go around. But how to I in all get the focus to transfer with a mouse click, etc?
One possible solution is to not use a KeyListener. Instead use Key Bindings which are much more forgiving with regard to focus. Have a look at the Key Bindings tutorials which Google can help you find in a jiffy.
For more specific help, consider creating and posting an sscce.

Retaining Focus for KeyboardListener

I have a JFrame which contains most everything in the application. It has a KeyListener attached, and it also has several buttons and textfield on it. The problem it, when a button is clicked or a textfield is selected, it gets focus and shortcuts don't work. Of course, one can tab out of them, but to do this you must tab through EVERYTHING (each button, each textfield) before giving the window focus again.
Is there a sensible way to only require one tab to return focus to the frame from the textfield, and no tabs to return focus to the frame from a button click?
A WindowListener doesn't seem like the best way to do this, but if it's the only way I suppose I can forge forward there.
Thanks in advance!
It has a KeyboardListener attached
I have never heard of the KeyboardListener class so I can only guess what you are trying to do.
My guess is that you should NOT be using a listener of any kind.
Instead you should be using Key Bindings.
If you only need this for the textfield, you can add a keyListener to the textfield and when the user presses tab use yourJFrame.requestFocus(). Otherwise refer you may want to use a window manager or a key map.

SWT: addFilter or addKeyListener on Composite

i want to have some composite-wide keyboard-shortcuts. The composites in question are in a tab-folder. I have a little function, which traverses all children of my composite and adds a KeyboardAdapter to every one of them.
The problem I have is that, when I open on of the tabs pressed keys aren't registered. I first have set the focus on some selectable widget in the tab, then it works. When i switch to another tab and then back, the focus is still kind of there (a grey selection instead of a blue one in a table for example), but it, again, doesn't work, until i click somewhere.
How can I do this? I thought about adding a filter to my display, but i only want events in a certain composite (and everything in there).
Thank you
Key events are delivered to the component that has keyboard focus. Composites do not get the keyboard focus, it is usually one of their child components that gets it and then they start receiving the key events (in case they are not used by children). Having the key listener on the parent Shell may possibly work.

Rectangular Java Swing Radio buttons?

I'd like to create a set of buttons in a Java Swing application like you get in a typical tool palette in a paint program. That is, a set of small square buttons, each containing an icon, only one of which is pressed down, and when you press another button, the first is deselected. I've thought of a number of solutions and none of them seem very easy/elegant.
This sounds like a job for JRadioButton, but if you add an Icon to that, you still get the small circle, which is fairly space inefficient. I guess an option would be finding an alternative Look and Feel or painting code for JRadioButton.
Another alternative might be to add JButton to a ButtonGroup, maybe setting a JToggleButton.ToggleButtonModel as the model, but that doesn't have the desired effect, as the painting code for a standard JButton does not keep it depressed when selected. Possibly the JButton code could be modified to do this. Like making it painting "selected" the same way as "pressed".
A third alternative would be to use normal JButton's, and add a common mouse listener that keeps them pressed or not, and communicates the changes between the buttons.
Can anyone advise on the best way to achieve the aim please? A simple method I've missed would be best, but advice on which of these three alternatives would be best and pointers on how to get started would be useful too.
What about a plain JToggleButton in a ButtonGroup? It is not abstract, you can instantiate one with an Icon, and it stays depressed while selected.
See the SwingSet2 demo:
http://java.sun.com/products/plugin/1.4/demos/jfc/SwingSet2/SwingSet2.html
Click the second icon on the toolbar (the one twith the check box and radio button) then tab "Radio buttons". Then click on "Paint Border" on the right panel, under "Display Options".
Source code of the demo is under your JDK install dir, so for example on my PC it's under \jdk1.6.0_01\demo\jfc\SwingSet2\src

Categories

Resources