Skip to main content

Handling keyboard events

4 replies [Last post]
tskale
Offline
Joined: 2005-08-17
Points: 0

I need to limit the number of characters user can enter in a text field. My approach is to build a custom TextField class as follows:

public class CTextField extends TextField {
attribute maxlen: Integer;
attribute onKeyTyped: function(event:KeyEvent)= function(event:KeyEvent) {
if(text.length() >= maxlen)
{
event.source.consume();
}
};
}

During compilation, the following error is reported:

error: Cannot override CTextField.onKeyTyped default initializer in onKeyTyped subclass.

How to handle the keyboard events? Is there any other method to limit characters in TextField?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tskale
Offline
Joined: 2005-08-17
Points: 0

TextField class should have an attribute such as maxlen that restricts number of characters allowed in the text box. This is a common data entry requirement. I shouldn't have to create a custom class to have this feature. Is there a place where feature requests could be submitted?

tball
Offline
Joined: 2003-06-10
Points: 0

All issues are filed using http://openjfx.java.sun.com/jira/.

shagus
Offline
Joined: 2007-05-10
Points: 0

Hi, try the following.

import javafx.ui.*;
public class CTextField extends TextField {
attribute maxlen: Integer;

override attribute onKeyTyped = function(event:KeyEvent) {

if(text.length() >= maxlen)
{
event.source.consume();
}

};

}

tskale
Offline
Joined: 2005-08-17
Points: 0

Thank you very much for your answer. It works as expected.