6.07 - Custom Components

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?

Figure - The Custom Component Application

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:

package ee402;

import java.awt.TextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class IntegerTextField extends TextField implements KeyListener {
        public IntegerTextField(int size){

        public void keyPressed(KeyEvent e) {}
        public void keyReleased(KeyEvent e) {}
        public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                        System.out.println("Numeric key pressed");
                else {
                        System.out.println("Non numeric key pressed");
  • We extend the TextField class so that the "IntegerTextField is a TextField".
  • The KeyListener allows us to capture key press events and requires us to implement the three methods: keyPressed(KeyEvent), keyReleased(KeyEvent) and keyTyped(KeyEvent).
  • We can find out which key was pressed within the keyTyped() method by using the e.getKeyChar() method. 
  • We then need to find out if the char is numeric (0-9) or one of the special keys, which are defined as static constants KeyEvent.VK_DELETE and KeyEvent.VK_BACK_SPACE.
  • If it is one of these keys then we will (temporarily) display the message "Numeric key pressed" if not then it must be a non-numeric key.
  • If it is a non-numeric key we print the message "Non numeric key pressed" and then we will consume the event using e.consume(), which will stop the event from being passed on for further processing - like cancelling the event.

And then to use this, we build an application as usual:


package ee402;

import java.awt.*;
import java.awt.event.*;

public class CustomComponentApp extends Frame implements ActionListener, WindowListener {

        private Button okay;
        private IntegerTextField intField;
        public CustomComponentApp()
         super("Custom Component");
         this.setLayout(new FlowLayout());
         intField = new IntegerTextField(20);

         this.okay = new Button("Okay");

        public void actionPerformed(ActionEvent e)
                if (e.getSource().equals(okay)){

        public static void main(String[] args) {
                new CustomComponentApp();

        public void windowActivated(WindowEvent arg0) {}
        public void windowClosed(WindowEvent arg0) {}
        public void windowClosing(WindowEvent arg0) {
        public void windowDeactivated(WindowEvent arg0) {}
        public void windowDeiconified(WindowEvent arg0) {}
        public void windowIconified(WindowEvent arg0) {}
        public void windowOpened(WindowEvent arg0) {}

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.
Derek Molloy,
17 Nov 2013, 10:25
Derek Molloy,
17 Nov 2013, 10:25