JTextField: unable to type after assigning a listener

Hi All,

I have a JTextField bean on a form which is set to be editable. It is ok until I call a method to assign a listener to the field.

var listener = new java.awt.event.KeyListener({keyTyped: globals.getKeyTyped()});
elements.bean_text.addKeyListener(listener);

After this code is executed the cursor is still blinking in the field but I’m unable to type anything.
The code seemed to work fine yesterday but I’ve been playing with it and did not save the working version (though I think it was the same).

What could go wrong?

Thanks,
Maria

P.S. If anything has been typed in the field before running that code, backspace will work in the field but looks like it’s the only key that works.

I think, you need to remove the parenthesis while assigning servoy method to the event.

var listener = new java.awt.event.KeyListener({keyTyped: globals.getKeyTyped});
elements.bean_text.addKeyListener(listener);

Infop:
I think, you need to remove the parenthesis while assigning servoy method to the event.

var listener = new java.awt.event.KeyListener({keyTyped: globals.getKeyTyped});

elements.bean_text.addKeyListener(listener);

Thanks for pointing that out for me, Infop. Now it works.

Remember to always dispose of your java listeners!
Especially in case of KeyListener since they can eat some memory and slow down your app significantly if you attach your listener more than once (it’s so easy to do, especially in developer where you launch and stop your client repeatedly)…

To avoid this pitfall, I usually do the following:

First, initialize the listener inside a form variable in the Form “onLoad” like this:

var listener = {}; // this is to tell Servoy that the "listener" var is an Object, otherwise you will get cast exceptions...

function onLoad() {
    var listener = new java.awt.event.KeyListener({keyTyped: globals.getKeyTyped});
}

then when I want to attach it, I put:

// remove first, if the listener wasn't already attached, this will do nothing:
elements.bean_text.removeKeyListener(listener);
// now I can attach my listener:
elements.bean_text.addKeyListener(listener);

And of course in the form “onUnload”, release the resources:

// remove:
elements.bean_text.removeKeyListener(listener);
// and release:
listener = null;

Thanks Patrick,
Good to know that.

Cheers,
Maria

Glad if it helps!

In the code above, there was a little typo, the onLoad method of the form should really read:

function onLoad() {
    listener = new java.awt.event.KeyListener({keyTyped: globals.getKeyTyped});
}

otherwise you will create a function-scoped variable instead of a form variable.

Sorry about that,

Noticed.
Just did not want to pick on you)

Thank you! :D :D

I have a related question: what if you want to pass on arguments via the listener?

Something like:

var listener = new java.awt.event.KeyListener( { keyTyped: globals.getKeyTyped(argument1,argument2) } )

This doesn’t work, but is there a way to pass on arguments?

As far as I know the only argument that is accepted by getKeyTyped() is the keyboard event but I might be unaware of something.
What would you pass as arguments?

Cheers,
Maria

Actually, i was poking around with a mouse listener. I wanted to pass on the source element on which the mouse had entered. But, I found out that reading the first argument (no matter if you didn’t pass on any arguments) already gives the source element!

Kaptan:
Actually, i was poking around with a mouse listener. I wanted to pass on the source element on which the mouse had entered. But, I found out that reading the first argument (no matter if you didn’t pass on any arguments) already gives the source element!

Cool, isn’t it :D

Um…
I can’t figure out the name of the calling element :(
arguments[0].source returns a java object javax.swing.plaf.basic.BasicComboBoxEditor$BorderlessTextField
But arguments[0].source.name is null, though the bean on the form (JComboBox) definitely has got a name.

Please help.

Cheers,
Maria

arguments[0].getElementName()

Doesn’t work: “Error during evaluation:TypeError: Cannot find function getElementName.”

Kaptan:

arguments[0].getElementName()

The argument you get is the KeyEvent, to get to the bean, you should do:

arguments[0].getSource()

So to get the (element)name of the bean, you can do:

arguments[0].getSource().getName()