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:


123456789101112131415161718192021222324252627
package ee402;

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

@SuppressWarnings("serial")
public class IntegerTextField extends TextField implements KeyListener {
        
        public IntegerTextField(int size){
                super(size);
                this.addKeyListener(this);
        }

        public void keyPressed(KeyEvent e) {}
        public void keyReleased(KeyEvent e) {}
        public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if(Character.isDigit(c)||c==KeyEvent.VK_DELETE||c==KeyEvent.VK_BACK_SPACE){
                        System.out.println("Numeric key pressed");
                }
                else {
                        System.out.println("Non numeric key pressed");
                        e.consume();
                }        
        }
}
  • 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:

CustomComponentApp.java

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
package ee402;

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

@SuppressWarnings("serial")
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");
         this.okay.addActionListener(this);

         this.add(intField);
         this.add(okay);
        
         this.pack();
         this.setVisible(true);
         this.addWindowListener(this);
        }
        
        public void actionPerformed(ActionEvent e)
        {
                if (e.getSource().equals(okay)){
                        this.intField.setText("0");
                }
        }

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

        public void windowActivated(WindowEvent arg0) {}
        public void windowClosed(WindowEvent arg0) {}
        public void windowClosing(WindowEvent arg0) {
                System.exit(0);
        }
        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.
ċ
CustomComponentApp.java
(1k)
Derek Molloy,
17 Nov 2013, 10:25
ċ
IntegerTextField.java
(1k)
Derek Molloy,
17 Nov 2013, 10:25
Comments