I'm trying to handle the event of pressing "enter button" when typing in a SWT Text field. Here is what I've done so far:
textField.addKeyListener(new KeyListener() {
#Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent arg0) {
if(arg0.keyCode == SWT.CR) {
//do something here....
}
}
});
This actually works when I hit Enter button, but not when I hit the enter button on the numpad.
Someone knows the constant variable for this button?
Thanks
Try using a traverse listener which is intended for handling things like Enter:
textField.addTraverseListener(new TraverseListener()
{
#Override
public void keyTraversed(final TraverseEvent event)
{
if (event.detail == SWT.TRAVERSE_RETURN)
{
...
}
}
});
I would try with:
if (arg0.keyCode==SWT.CR || arg0.keyCode==SWT.KEYPAD_CR)
Related
I have a series of buttons in my code. I have added key listeners to individual buttons to listen to keys, so that when user presses RIGHT, LEFT,UP DOWN, I can transfer focus to the next button.
Note: I know that TAB can be used
now everything works really great! but when the focus is at a disabled button. I am not able to listen to it.
Any suggestions, as to how I can go around the problem?
Please pardon me before hand for amateur coding style!
addEntry.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{calPeriod.setFocusable(true);
calPeriod.grabFocus();
}if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
getTime.setFocusable(true);
getTime.grabFocus();
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
genChart.setFocusable(true);
genChart.grabFocus();
}
}
});
I just started working on Gwt2.0. I have two textbox here. Both perform the same event operation. Using
addFocusListener()
What I have now is.
areaBox.addFocusListener(new FocusListener(){
#Override
#Deprecated
public void onFocus(Widget arg0) {
// TODO Auto-generated method stub
areaBox.setTitle("Area");
}
#Override
public void onLostFocus(Widget arg0) {
if(areaBox.getText().length() >= 4 )
{
areaBox.setStyleName("gwt-TextBox-Success");
}
else
{
areaBox.setStyleName("gwt-TextBox-Error");
cityBox.setText("Enter Area Name ");
areaBox.addFocusListener(new FocusListener(){
#Override
#Deprecated
public void onFocus(Widget arg0) {
areaBox.setText(null);
areaBox.setStyleName("gwt-TextBox-AE");
}
#Override
#Deprecated
public void onLostFocus(Widget arg0) {
}
});
}
}
});
cityBox.addFocusListener(new FocusListener(){
#Override
#Deprecated
public void onFocus(Widget arg0) {
cityBox.setTitle("City");
}
#Override
public void onLostFocus(Widget arg0) {
if(cityBox.getText().length() >= 4 )
{
cityBox.setStyleName("gwt-TextBox-Success");
}
else
{
cityBox.setStyleName("gwt-TextBox-Error");
cityBox.setText("Enter City Name ");
cityBox.addFocusListener(new FocusListener(){
#Override
#Deprecated
public void onFocus(Widget arg0) {
cityBox.setText(null);
cityBox.setStyleName("gwt-TextBox-AE");
}
#Override
#Deprecated
public void onLostFocus(Widget arg0) {
// TODO Auto-generated method stub
}
});
}
}
});
What the code does is. When Focused on Area TextBox, a tooltip appears, showing what to enter. When Focus on it is lost or comes to next textbox i.e City TextBox, it checks whether the entered string is greater than 4 characters. if yes then SUCCESS css style is applied to the box, if not ERROR css style is applied. So, when clicked on it(AreaBox) again, The text is cleared and css style is reset.
The above is a sample between 2 textbox.
Please help me, I have nearly 10 such fields, I want to minimize the code.
I am thinking of using collections or custom widgets. But don't know where to start. Need your help and opinion. Thanks...
Here is how I would solve this problem:
areaBox.addFocusListener(generateFocusListener("Area", "Enter Area Name "));
cityBox.addFocusListener(generateFocusListener("City", "Enter City Name "));
FocusListener generateFocusListener(final String title, final String text) {
return new FocusListener(){
#Override
#Deprecated
public void onFocus(Widget widget) {
// initial focus happens here
TextBox box = (TextBox) widget;
box.setTitle(title);
}
#Override
public void onLostFocus(Widget widget) {
// Focus is lost for the first time
TextBox box = (TextBox) widget;
if(box.getText().length() >= 4 )
{
box.setStyleName("gwt-TextBox-Success");
// Change focus listener so that now once pressed it resets
box.addFocusListener(new FocusListener(){
#Override
#Deprecated
public void onFocus(Widget widget) {
TextBox box = (TextBox) widget;
box.setText(null);
box.setStyleName("gwt-TextBox-AE");
}
#Override
#Deprecated
public void onLostFocus(Widget widget) {
TextBox box = (TextBox) widget;
if(box.getText().length() >= 4 )
{
box.setStyleName("gwt-TextBox-Success");
}
else
{
box.setStyleName("gwt-TextBox-Error");
box.setText(text);
}
}
});
}
else
{
box.setStyleName("gwt-TextBox-Error");
box.setText(text);
}
}
};
}
The title might be a little misleading, didnt know how to put my problem short.
Basically what im doing is im using keyboardlistener to find out which keys are down and according to that im moving my game character.
The problem is, when you click out of the window, while holding down a key my listener doesnt register the keyReleased event.
I tried to fix it by using mouse listener and the mouseExited event, but that doesnt fix it all the time, sometimes it does sometimes it doesnt.
Heres my implementation:
Keyboard:
public void mouseLeftWindow()
{
for(int i =0;i<KEY_COUNT;i++)
{
keys[i] = false;
}
}
#Override
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if(keyCode>=0 && keyCode<KEY_COUNT)
{
keys[keyCode] = true;
}
}
#Override
public void keyReleased(KeyEvent e)
{
int keyCode = e.getKeyCode();
if(keyCode>=0 && keyCode<KEY_COUNT)
{
keys[keyCode] = false;
}
}
where keys[] is a boolean[] describing, which codes are pressed
mouse:
#Override
public void mouseExited(MouseEvent e)
{
mouseMoved(e);
keyboard.mouseLeftWindow();
}
Your program will listen for further key events even when your mouse exited the component. That means you set everything to false on exit but if a key is still pressed it will be set to true immediately again. I think you are looking for a FocusListener instead of a MouseListener.
addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
}
#Override
public void focusLost(FocusEvent e) {
keyboard.mouseLeftWindow();
}
});
I have a document listener that works just fine. However, I'd like to add some functionality to it so that when the user hits the Enter key, the focus shifts to another object. I can't figure out how to trap this. Here is my code:
txtNum1.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent e) {
setAnswer(e);
}
#Override
public void removeUpdate(DocumentEvent e) {
setAnswer(e);
}
#Override
public void changedUpdate(DocumentEvent e) {
setAnswer(e);
}
private void setAnswer(DocumentEvent e) {
if (txtNum1.getText().equals("")) {
num1 = 0;
} else {
num1 = Integer.parseInt(txtNum1.getText());
}
calcAnswer();
System.out.println(e); //trying to output the event 'Enter'
}
I can do this with a key listener, but I've been scolded on this site before about using that approach, so I'm trying to learn this the correct way.
Thanks!
EDIT:
Per the suggestions below, I added the following code, but it seems to have no effect. Can anyone see what I am missing? Thanks!
/* If the user hits the Enter key, we want the focus to shift to
* the next text field */
txtNum1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
txtNum2.requestFocus();
}
});
On a JTextfield, you can trap the Enter key simply by adding an ActionListener. It will get fired when the users types enter
my layout like :
i want focus at Company Name Texfield when i press TAB button at first time , but right now i get focus at Add Button how can i manage it ?
i try code like
ChangeFocus(mTextFieldCompanyName, mTextAreaAboutUs);
ChangeFocus(mTextAreaAboutUs, mTextAreaContactUs);
ChangeFocus(mTextAreaContactUs, mButtonVideo);
ChangeFocus(mButtonVideo, mButtonImage);
ChangeFocus(mButtonImage, mButtonSave);
public void ChangeFocus(Control mControlFrom,final Control mControlTo)
{
mControlFrom.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event)
{
if (event.getCode() == KeyCode.TAB)
{
System.out.println("TAB pressed");
mControlTo.requestFocus();
event.consume(); // do nothing
}
}
});
}
Try wrapping your mTextFieldCompanyName inside Platform.runLater()
Platform.runLater(new Runnable() {
#Override
public void run() {
mTextFieldCompanyName.requestFocus();
}
});
Hope it helps :)