April 19, 2019
Hot Topics:

Dynamic Error Notification and Construction with Java Swing

  • May 9, 2005
  • By Vlad Kofman
  • Send Email »
  • More Articles »

Note the IP is also checked for correctness with a regular expression. Because the message is dynamic, it will be different depending on the errors found:

Implementing this technique solved both problems of multi pop-ups and code reuse, and it can be applied to GUIs of any complexity and size. The resulting array of error massages can be passed or used in any context and not necessarily in JDialogs.

Adding Multiple Action Listeners

The validation was initialized when the user clicks on the "NEXT" button because it has an action listener associated with it. If the requirements say that the user needs to be able to simply press the Enter key while the focus is on any field, we need to add a key listener to every single text field. This would require adding some Key adapter or implementing a key listener interface and then checking whether the key pressed was indeed an "Enter" key. Besides, every text filed also would need to register to generate key events. Luckily, there is another way.

Java 2 provides an extremely useful feature built in specifically for capturing an Enter key press on the text fields. Simply attaching an action listener to any text field component would generate an action event if Enter was pressed on it. However, this still necessitates explicitly attaching an action listener to every field.

To solve this, I interrogated the parent component panel for the list of all text fields added to it and, as I traversed through them, I attached the listeners dynamically.

Component fields[] = this.getComponents();
   for (int i = 0; i < fields.length; i++) {
      if (fields[i] instanceof JTextField) {
         ((JTextField) fields[i]).addActionListener(this);

This method can also be used to add any kind of listeners to GUIs of any complexity—because you can recursively navigate down if the component in scope is in another container.

For instance, passing a top parent GUI container to this method will recursively add action listeners to all text fields, even those that are added to the child containers.

public void attachAnyListener(JComponent component){
   Component fields[] = component.getComponents();
      for (int i = 0; i < fields.length; i++) {
         if (fields[i] instanceof JComponent) {
         if (fields[i] instanceof JTextField) {
               ((JTextField) fields[i]).addActionListener(this);
         //other listener attachments here
Note: The recursive mechanism also can be used to detect any changes to the user-entered information in large GUIs by using focus listeners or change listeners. Eventually, you prompt the user whether changes need to be saved on exit.

After attaching action listeners to the text fields, the application will start checking all fields. It doesn't just check the "NEXT" button press; it also checks on an "Enter" key press on any text field by calling checkFields() in the actionPerformed method.

The last validation check was for the IP address format. I used a regular expression feature available in the matches() String class method. The field is checked by a pattern of the simple IPv4 format that can be only 1 to 3 digits followed by a period, repeating 3 times, and followed by another set of 1 to 3 digits.


The regular expressions (regex) are built into Java and even have a separate package in JDK java.util.regex. The Sting class uses pattern matching capabilities of regular expressions in several methods (please see the JDK API documentation for more information). The regex are very powerful and can allow for very complex matching, such as "^(([^:/?#]+):)?((//([^/?#]*))?([^?#]*)(\\?([^#]*))?)?(#(.*))?" or something even worse looking.


In this article, I've shown how to dynamically generate notification/validation messages. The technique can be applied to any GUI developed in Swing. I have also described how to add action listeners to any component implicitly and validate data using regular expressions. Even though the validation and notification is specific to the Java Swing toolkit, the technique can be applied to the most modern GUI toolkits.

Download the Code

You can download the source code used in this article here.


Java Swing, 2nd Edition by Marc Loy, Robert Eckstein, Dave Wood, James Elliott, and Brian Cole. November 2002. ISBN: 0-596-00408-7.

The Java Developers Almanac 1.4: http://javaalmanac.com/egs/java.util.regex/pkg.html

About the Author

Vlad Kofman is a System Architect working on projects under government defense contracts. He has also been involved with enterprise-level projects for major Wall Street firms and the U.S. government. His main interests are object-oriented programming methodologies and design patterns.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date