How to set traversal order by ENTER in SWT - java

In SWT, we can set the tab oder by using the code below:
composite.setTabList(new Control[]{button1, button3});
Is there any way to change the key traversal from Tab to Enter? My application connect to a bar code reader and it just support Enter key after read the barcode

There is a TraverseListener in SWT that can be used to change the effect of traversal keys.
For example, a traverse listener can be used to focus the next field on Enter like this:
text.addTraverseListener(new TraverseListener() {
#Override
public void keyTraversed(TraverseEvent event) {
if (event.detail == SWT.TRAVERSE_RETURN) {
event.doit = false;
// focus next control
}
}
});
Setting the doit flag of the event to false consumes the event and prevents it from causing the default action - if any. In a multi-line text field, the Enter key may begin a new line unless the event was consumed by a listener.

Related

Why does Android Soft Keyboard close when I press "Enter" (and not make a newline)

The android soft keyboard is toggled from the context menu via
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
It types text fine, but when the Enter is pressed, the keyboard closes. Nothing is capture in onKeyDown(int keyCode, KeyEvent event)
The layout looks like this.
Extra Info: For those wondering, the keyboard input is being sent over a TCP connection, not into any view within the layout.
Check your xml may be you set maxline=1 or may be give singleLine="true"
Alright so this question got no attention but I'm going to put my solution here in case someone else has this problem.
For some reason Android has decided that after typing text (with no EditText around) the Enter key is now an action button and not a newline key.
I managed to capture input by receiving key presses in dispatchKeyEvent() rather than onKeyPress()
#Override
public boolean dispatchKeyEvent(KeyEvent ke)
{
int unicode = ke.getUnicodeChar();
if (ke.getAction() == 0 && ke.getKeyCode() == KeyEvent.KEYCODE_ENTER) // getAction() returns 1 (up) or 0 (down).
{
// do my work here
return true; // end here (doesn't go to onKeyDown())
} else
return super.dispatchKeyEvent(ke);
}
this event is fired twice on key press so it was important to use getAction() in the if statement. For all other keyboard presses, I used onKeyDown() which is why it was important to return super.dispatchKeyEvent(ke); if it wasn't the Enter key.

JTable: change traversal order

I want to traverse horizontally through a JTable, when pressing enter. I've tried with JTable.changeSelection but it doesnt seem to work. Any ideas how to change the traversal behavior?
Read up on Key Bindings. You would want to "share an Action with a different KeyStroke"
Enter is vertical traverse , TAB is horisontal, just hold Enter event and generate Tab event or call function for Tab Event. But you should set up next properties:
table.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
JTable.getInputMap(JInternalFrame.WHEN_ANCESTOR_OF _FOCUSED_COMPONENT)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "SOME_ACTION");
JTable.getInputMap(JInternalFrame.WHEN_FOCUSED)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "SOME_ACTION");
JTable.getActionMap().put("SOME_ACTION", actions);
actions = new AbstractAction() {
public void actionPerformed(ActionEvent ae) {
//This action will get fired on Enter Key
}
};

Restricting number of characters in a message in JTextPane

I have a JTextPane where I want to restrict the user to enter a message with only 200 characters. So, I have a KeyListener which listens for a Keyevent and checks for a KeyEvent. If the message is more than 200 characters, a JOptionPane.showMessageDialog is shown to display a warning to the user. This bit works fine.
The problem is that once the warning is displayed and the users clicks on 'OK' he can only use the Backspace key in the JTextPane. I want the user to be able to use the delete key, the arrow keys, the shift and control keys to be able to select the text to be deleted.
Can anybody suggest a way of achieving this??
// Add Key Listener to Send Field
chatEditorKeyListener = new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
checkKeystroke(e);
}
};
private void checkKeystroke(KeyEvent e)
{
//Check if enter or back space is entered
if( e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_ENTER )
{
// user is typing, so test the size as we go and report when we hit boundary
String text = messageBox.getText();
if(text.length() > maxMessageSize)
{
showAlertBox();
}
}
else if ( e.getKeyCode() == KeyEvent.VK_ENTER)
{
//User sending the message
e.consume();
String text = messageBox.getText();
if(text.length() > maxMessageSize)
{
showAlertBox();
}
Drag-and-drop. Copy-and-paste. Accessibility input methods. There are many reasons why this approach is not appropriate.
Instead restrict the contents through the Document. Set a DocumentFilter through AbstractDocument.setDocumentFilter so that you do not need to subclass or implement the document.
A pop up is not great for user experience. Be more subtle. Not allowing any more character will do (please don't beep!). Possibly add a countdown as twitter and stackoverflow do.
Read the section from the Swing tutorial on "Text Component Features" which contains a section on "Implementing a Document Filter" that does exactly what you want.
Test if the current size+1 hits the boundary, consume the event and show the message box.
It's important to comsume the event to never actually oversize your text box!

Java SWT: apply key events from List to Text field

I am making a component in SWT that contains a text field and a list. Whenever text is entered it filters the list. So far everything is working great and I am just trying to add some nice usability features.
What I want to do is listen for any key events in the List field, if it is Enter that is pressed I perform the 'ok' action (already done) but otherwise I want focus to change to the text field and have the key event triggered there. Basically, if the focus is on the List field and the user types something I want it to be automatically typed into the text field.
Responding to the keyPressed or keyReleased event is fine for setting the focus to the text field, but I need to then repeat the keyEvent somehow so that whatever was typed is actually entered. Any ideas?
So this is what I did:
itemList.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if (e.keyCode == '\r' || e.keyCode == SWT.KEYPAD_CR) {
okButtonAction();
} else if (e.keyCode == SWT.ARROW_UP || e.keyCode == SWT.ARROW_DOWN || e.keyCode == SWT.ARROW_LEFT || e.keyCode == SWT.ARROW_RIGHT) {
super.keyReleased(e);
} else if (e.character > 0) {
filterInput.setFocus();
Event event = new Event();
event.type = SWT.KeyDown;
event.keyCode = e.keyCode;
event.character = e.character;
Display.getCurrent().post(event);
try {
Thread.sleep(10);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
event.type = SWT.KeyUp;
Display.getCurrent().post(event);
}
}
});
I read that the Display.post method was there for doing automatic GUI testing, but it works for my purpose here, so I will use it unless anyone can give me a good reason why not??
My first idea was that there may be a way to re-fire the key event (or fire a copy of it) to the text field. But that may not yield the desired result, because there are some key events you probably don't want transferred from the list to the text, e.g. pressing the Up and Down arrow keys for navigation within the list.
So you should decide which key events trigger the focus transfer. From your question, I understand that you are implementing a text filter, so you should restrict the transfer to text characters. Once you know the character that was entered, you may append it to the filter text manually (or insert at the cursor position of the text field).

Use Tab-key on a multiline text filed in swt?

How do I prevent a multi-line text field from "stealing" tab-key presses?
I mean: I'd like to use TAB to cycle between the elements of a window, but when I enter the multiline text, TAB becomes a "normal" key, and simply inserts tabulators into the text I'm typing.
How do I handle this? Should I write some custom listener, or can I change the behaviour of the component by using an SWT constant?
SWT defines a mechanism for implementing this kind of behaviour using the TraverseListener class. The following snippet (taken from here) shows how:
Text text1 = new Text(shell, SWT.MULTI | SWT.WRAP);
text1.setBounds(10,10,150,50);
text1.setText("Tab will traverse out from here.");
text1.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
e.doit = true;
}
}
});
Add a keystroke listener to the textfield or textarea and implement a special handler for TAB. I've seen this 'tab catching' on Swing applications too and it's usually annoying.
You could offer another handler for Shift-TAB which then would insert a \t, if you still want to allow tabs in the textfield.

Categories