I am using my predefined inherited Focus Traversal Class For My JFrame
I have defined the key press event for one of my button with some action on pressing Tab key to select other tab of my jTabbed Pane . This button is not responding only for the tab key .
int index=1;
if(evt.getKeyCode() == KeyEvent.VK_TAB)
{
// wrap around
if(evt.isShiftDown())
{
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusPreviousComponent();
}
else
{
System.out.print("Shift Up");
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
jtabPaneProducts.setSelectedIndex(index);
}
}
Please guide me how can i made jbutton to respond to the TAB key press in addition to focus traversal functionality.
You should be interested to read How to Write a Key Listener:
Alternatively, you can use the KeyEventDispatcher class to pre-listen to all key events. The focus page has detailed information on the focus subsystem.
And consequently: Interface KeyEventDispatcher
Related
Context: JDK 8 & JavaFX
I have a TextField control that is used in a dialog. It is the first edit control, so it gets the focus when the dialog opens. The dialog has a button configured as the cancel button (Button.setCancelButton(true))
With a plain TextField, if I hit ESC immediately after the dialog opens, the dialog is closed (as expected).
However, once I add a TextFormatter with input filter to the TextField, the ESC keypress appears to be being consumed by the input control and ESC no longer closes the dialog.
The TextFormatter only has an input filter (to restrict the input control to just digits), but the input filter does not get invoked on the ESC keypress - because the content of the field has not changed.
It's a fairly minor issue, but it's annoying not having consistent behaviour, and not being able to just hit ESC to dismiss the dialog. Any ideas on how to ensure that the ESC keypress is propagated/not consumed, so that it is handled by the dialog?
Edit:
My question appears to be a duplicate of this one: Escape from a Number TextField in a JavaFX dialog. Which of course I failed to find despite trawling through Google before posting... TLDR; the TextFormatter class fails to forward the ESC keypress event on.
I think the easiest approach is to avoid trying to “fix” the TextField and TextFormatter, and just add a key listener:
textField.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ESCAPE) {
dialog.setResult(ButtonType.CANCEL);
}
});
If the Dialog is not an Alert (or more precisely, is not a Dialog<ButtonType>), you can locate the button and activate it yourself:
textField.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ESCAPE) {
Button cancelButton = (Button)
dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
cancelButton.fire();
}
});
I'm trying to test by SWTBot hyperlinks inside my Eclipse's text editor. The problem is that the hyperlinks are shown on demand (an Eclipse feature), meaning - the token changes is revealed as hyperLink only when mouse moves over it + a keyboard key (Ctrl or Alt) is pressed.
How can I simulate in SWTBot the mouse-move together with a key pressed?
When mouse moves over a link, then a MouseEvent is generated. Some MouseMotionListener (or maybe MouseListener) consumes this event and then shows hiperlink for you.
You can simulate this event:
Component source = null; // TODO set up a valid component
MouseEvent event = new MouseEvent(source, MouseEvent.MOUSE_ENTERED, System.currentTimeMillis(), InputEvent.ALT_DOWN_MASK, source.getX(), source.getY(), 0, false);
MouseMotionListener[] mouseMotionListeners = source.getMouseMotionListeners();
if (mouseMotionListeners!= null && mouseMotionListeners.length > 0) {
MouseMotionListener mouseMotionListener = mouseMotionListeners[0];
mouseMotionListener.mouseMoved(event);
}
The InputEvent.ALT_DOWN_MASK in the constructor means that Alt is pressed.
Note that you should define what Component responsible for consuming events in your case.
You can find more information in the tutorial How to Write a Mouse Listener and MouseEvent API
I am using a Table component in SWT. Whenever I edit a value in this table and press enter, this value is saved in the text component in this table.
But when I want to enter 2 words seperated with a TAB between them, then the editor loses focus and moves on to the next cell to edit(like pressing tab in a browser form). I don't want this to happen and let my users enter tabs between words without the focus getting lost. Anyone have an idea how to create this?
I allready tried using a keyListener, but it seems the tab event isn't even processed by this listener
You could add a TraverseListener to your textField.
text.addTraverseListener(new TraverseListener () {
public void keyTraversed(TraverseEvent e) {
switch (e.detail) {
case SWT.TRAVERSE_TAB_NEXT:
case SWT.TRAVERSE_TAB_PREVIOUS: {
e.doit = false;
}
}
}
});
Check out this example code snippet.
I have a swing application with multiple jtextfield on it. How do you replace the function of the enter key wherein when you press the Enter key, it will transfer to the nextfocusable component just like the tab key? I dont want to put a keylistener on each jtextfield.
You're looking for Container.setFocusTraversalKeys:
Container root = ...
// pressed TAB, control pressed TAB
Set<AWTKeyStroke> defaultKeys = root.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
// since defaultKeys is unmodifiable
Set<AWTKeyStroke> newKeys = new HashSet<>(defaultKeys);
newKeys.add(KeyStroke.getKeyStroke("pressed ENTER"));
root.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newKeys);
For more information, take a look at the Focus Subsystem tutorial.
You can call:
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.focusNextComponent();
but you will have to register a single ActionListener with all your JTextFields.
I on the keyboard a lot more buttons, and I really need to listen to push the buttons (multimedia play, stop ..).
How do it?
And I would like to catch the event, even when the window is minimized.
Use this jintellitype to catch key events outside your app in Windows.
Here is for linux JXGrabKey
Update: to use multimedia buttons, you need to know it's codes. Add this listener to your app's frame to find out codes:
class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent evt) {
System.out.println("Key code: " + evt.getKeyCode());
}
}
If you will know the code, just check if evt.getKeyCode() is what you need and make some actions.