Because we are using a using Java, which is an object-oriented programming language, we can take any class, extend it with our own functionality or replace some of its functionality or behaviour. We have already met the java.awt.TextField class, which allows you to enter a string in a text field container. Say we wanted to develop our own TextField ( java.awt.TextField) component that only allows you to enter in numeric values (i.e. no letters), but also allow us to press the BACK SPACE key or the DELETE key on the keyboard, how can we do this?Well the first step is to extend the java.awt.TextField component and add our custom behaviour in a new class called IntegerTextField that is defined below:
CustomComponentApp.java
You can see in this example that the new IntegerTextField behaves exactly like a regular TextField , except in the application you cannot press keys other than 0 to 9 and the BACKSPACE and DELETE keys.These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way. |