How do I keep keyboard focus on a single component? - java

I need to keep keyboard input focus on a single component inside a JPanel. It's for an application with a on-screen-keyboard.

Not sure I really understand the question. But you can try something like:
otherComponents.setFocusable( false );
You also might need to use a custom FocusTraversalPolicy.
If you need more help then post a SSCCE that demonstrates the problem.

You better put the focus back to the component (component.grabFocus()) after pressing a button on the on-screen keyboard.
Or you could set focus listener (component.addFocusListener(FocusListener l)) and never let go of the focus by calling grabFocus() in the focusLost() method of FocusListener.

This will give you focus on your singleComponent when opening your Frame, without having to change the focus policy of enything else: singleComponent.requestFocusInWindow(); Since the focus will not freeze, you will need to setFocusable(false) for the other components like camicr suggests.

Just a guess: take a look at the InputVerifier.

Related

How to create my custom SWT control selectable?

I can make control extending Composite or Canvas.
How to make it selectable? I.e. how to make it behave like Button? Button is disabled for extend. I see a lot of unportable code inside it.
So how to make Button-like control of myself?
Should I process mouse and keyboard events myself or there is some premade functionality to utilize?
You will have to handle the events yourself. I'll give you a couple of hints and references here:
First of all, make sure you read this: Creating Your Own Widgets using SWT
Have a look at SquareButton. It's a custom Button widget and should contain all the code you need.
Here is a very related SO question.
Hope this helps.
Canvas is for drawing from "inside". So, you have to create your own UI input. As here.

How do I lose focus on a JComboBox?

I have a JComboBox with an key listener.
When I hit <enter>, I fire off some action, and then I need to to lose focus on the JComboBox!
To focus on it, I can do JComboBoxObject.grabFocus();
But doing transferFocus() to get the focus to a next element (I don't care WHERE the focus goes, just away from combo box) does NOT work.
Doing grabFocus() from another combo box works, but seems like a pretty annoying hack to me. Is there a better solution?
I can suggest you to first use the
.getNextFocusableComponent()
and then use the
.requestFocusInWindow()
that means Implementing it like this,
JComboBox.getNextFocusableComponent().requestFocusInWindow();
One important note is that .getNextFocusableComponent() has become obsolete but it can work really better, you can use it but If you have any other solution, I would prefer not using this.
Updated: Starting from this two-combo example, adding either of these lines to the actionPerformed() implementation seems to do what you want.
combo1.transferFocus();
combo2.requestFocusInWindow();

java - deactivate a listener

I have a general question regarding listeners.
Lets say I have two JTabbedPanes and both have a ChangeListener. They are both displayed and I want them both to show the same pane (index) so when a user changes the selected pane in one the other changes too.
In brief, one JTabbedPane listener changes the other JTabbedPane using setSelectedTab().
Obviously, the first listener will activate the second listener and the second will reactivate the first in an endless operation.
This will be solved with booleans.
Is there a smarter way to do it?
Is there a way to change a tab without triggering the Listener?
Is there a way to activate the listener only when a user changes it and not the code?
Thank you.
BTW: I always have the same questions with buttons. But with buttons I take the code from the listener and put it in a method. when One button needs to activate a button it calls its code. But in JTabbedPane it is different.
The simple solution is to act only when necessary. For example:
if(currentTab != desiredTab) {
// change tab
}
That will prevent an infinite loop.
If you need to be able to switch the behavior on and off, then using a boolean flag isn't a bad way to go about it. The alternative is the remove the listener, using removeChangeListener. The flag may be more performant as it may avoid memory allocation and deallocation, but a lot depends on the other details of your situation.
share the selectionModel, like
secondTabbedPane.setModel(otherTabbedPane.getModel());

Adding MouseListener breaks Keylistener

When I remove all of the MouseListener components and run the applet I have to click on it to give it focus, then the KeyListener works perfectly. To solve this I added an "opening page" where you have to click on start before the game runs. This would give the applet focus and not start until the player was ready at the same time. I add in the MouseListener and it works great, but now the KeyListener does not register at all. I can not find out why, but I imagine it has something to do with focus again. Anyone know what should be done here?
Code can be found here: http://pastebin.com/LDxtk878
Thanks!
Without examining all 322 lines of your code, you might look at the article How to Write a Key Listener with attention to the section following "Note: To fire keyboard events, a component must have the keyboard focus."
Also, consider extending JApplet, as an alternative.

Capturing the close tab event of a JTabbedPanel in Java Swing

Is it possible to capture the close tab event for a JTabbedPanel in Java Swing.
I want to check for some conditions and if they are not met, then I have to prevent the user from closing it.
Thanks!
Update: I created a custom event, based on this code and it solved my problem.
Sun's tutorial on Tabbed Panes has an example with close buttons on each tab. If you look a the source of the example you can see it reacting to the close clicked.
Edit: ButtonTabComponent. It has an inner class that extends JButton.
Based on your comment, do you already have something in place that closes Tabs? What are you doing to achieve this?
I created a custom event, based on this code and it solved my problem.

Categories