JFrame continuing to receive key strokes even after seeming to lose focus - java

I have a Java app that I'll call App. App will occasionally display a JFrame that we'll call myFrame. App will also display a JTextArea that is contained in either a JDialog or a JFrame (I'm not sure which, but I can find out if that's necessary to answer this question). Let's call this JTextArea "myTextArea".
Now, the following sequence of events happens:
We display myFrame. It has the focus and you can give it input.
We call myFrame.setVisible(false)
We display myTextArea.
We call myTextArea.requestFocus().
myTextArea has the focus (the cursor is blinking with in it), but all the keystrokes that are input are sent to myFrame!
Note that myTextArea is not contained in myFrame.
What is going on here? Has anyone heard of a non-visible JFrame receiving keystrokes? Not only receiving keystrokes but stealing them from some other component that has the focus?

I found what’s basically causing the problem. MyFrame has a class MyKeyEventHandler that implements KeyEventDispatcher. The method dispatchKeyEvent(KeyEvent e) is always returning false even for key strokes that are intended for myTextArea. Therefore the key strokes do not reach myTextArea.

It is not about toggling the visibility. The JFrame is initialized first and still has focus. You are only making it invisible, not taking away the focus from it.
Moreover, your JTextBox needs to have a parent container. Possibly
myFrame.add(myTextArea);
should work. To shift the focus to the JTextArea, use :
myTextArea.requestFocus();

Related

Two JPanels, requesting focus and Keyboard input

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

(Java) Can I use KeyListener to input text? (inside event dispatching thread)

I'm building a Swing game that is drawn entirely (everything, menus included) in single JPanel object. I have a game loop in this JPanel, which handles everything from updating the state of the game to drawing it and so on.
Now, I need to somehow enable the player to type in their own text in some of the menus. Creating a new player profile, saving some other information and the like. I can't seem to find a good way for doing this. Using a Scanner is obviously out of the question, since doing that will interrupt the event dispatching thread, freezing the game on the spot (I learned that the hard way).
I have concluded that the only way to do this is to use the KeyListener to somehow record the keys I have pressed on the keyboard. I have been experimenting with its keyTyped method, but my results have been poor. I have been able to make it record my keypresses, but the problem is, it records EVERY key I have pressed, including backspace, control, TAB, and so on... Plus, every character that I type is outputted capitalized, regardless of whether I have Caps Lock on or not.
I hope you understand what I'm trying to achieve here. So my real question: Is there any easy way to record typing using KeyListener? Or is there some other way that can be used inside of the event dispatching thread?
Your options are:
Use a standard popup dialog and get your text that way
Embed a text field into your panel to receive input
The problem is that a KeyListener is just that, it listens for key events. The standard text input components combine this with a DocumentListener for processing text

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.

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