September 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Dynamic Error Notification and Construction with Java Swing

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

Any type of Graphical User Interface (GUI) application, designed based on any functional requirement, will assume user interaction as part of its standard functionality. Therefore, almost all GUIs allow users to enter data and manipulate it. After the information is gathered from the user, it typically needs to be either stored, locally or remotely, or used in the application in real time. But, before doing something with the data, applications usually need to check it for correctness. In this article, I will concentrate on form validation and dynamic creation of notification messages in Java Swing GUI applications. I will also go over a few useful tips for GUI developers such as adding listeners to multiple components and using regular expressions for easy validations.

Because Java/J2EE became de facto for enterprise rapid development and deployment environments, especially in the financial industry, the Java Swing framework became a favorite choice for desktop applications as well. With Swing, programmers can create extremely robust GUI applications in a short period of time and deploy them on any platform, without recompilation or any other changes for that matter. The applications will behave exactly the same on multiple platforms, and the only downside will be a need to have the Java Runtime Environment (JRE) installed on the target machines.

User Form

To demonstrate user form validation, I created a small application based on fictional requirements that entail a user entry screen that gathers rudimentary information, such as first and last name, and machine IP address. A form with several text fields takes user data and, after checking it, allows the user to proceed.

The fictional requirements also say that user may want to press the "Enter" key after entering data in any field to proceed, in addition to being able to click on the "Next" button. Moreover, the IP address needs to be checked for correct format (IPv4).

Dynamic Validation

Although the basics of creating the Swing GUI is beyond the scope of this article, you should be familiar with creating simple interfaces, using any layout manager either manually or with an IDE. For this article, I have used JBuilderX to create a frame with GridBagLayout and add all the required components. The form in the sample application is trivial because it contains only a few labels and text fields. Nevertheless, validation needs to be done on every component and the technique to do it can be applied to any GUI, no matter how complex. For instance, all fields need to be filled before the user can proceed and the IP field needs to have an additional check for the correct format.

The simplest way to check fields would be to add an action that listens to the "NEXT" button, and when a new action event is fired (on button press), check every field one by one for correctness and display an error message if something is wrong.

Ex. method checkFields () {    // pseudo code
   if (field1 is not right)
   show error
   if (field2 is not right)
   show new error
   ... etc ...
}

The problem with this approach is that when multiple fields are incorrect, the user would see a lot of error massages at the same time—several pop-ups, for instance. Another problem is that every field needs to have its own code to show the error message. Therefore, if the logic to display errors is not as simple as printing out to an output stream somewhere, it would have to be redundantly used to display every error statement.

In this case, the requirement is to show a pop-up dialog such as JOptionPane.showMessageDialog.

The solution to having multiple dialogs pop up at the same time could be breaking out of the check method after the first error was found, but this still does not solve the redundancy of implementing and showing a new dialog for every error condition. Redundancy can become abundant if the GUI is very complex.

So, what is the graceful solution?

An important, not well known, property of Java dialogs is that they can take an array of an object as a parameter to display multi-line messages. For example:

JOptionPane.showMessageDialog(this, new String[]{"Hello", "World"});

You can even pass in objects with different text properties color, font, and so forth. The solution, therefore, is to display only one dialog at the end of the validation routine and pass in an array of error messages. The array would be dynamically created based on errors found and the user would see all of them in one pop-up.

In the case of my simple application, the code looks like this; a new array list is initialized and, on every error condition, a string message is added to it. At the end, if array is empty, nothing is displayed; if it's not empty, it is passed to the dialog as a parameter, and a dynamic pop-up is shown.

private boolean checkFields() {
   // init array
   ArrayList msg = new ArrayList(0);
   // check if first name is blank
   if (getRoleNameTF().getText() == null ||
      getRoleNameTF().getText().equals("")) {
      msg.add("Please enter First Name Field - it is required!");
   }
   // check if last name is blank
   if (getRoleURNTF().getText() == null ||
      getRoleURNTF().getText().equals("")) {
      msg.add("Please enter Last Name Field - it is required!");
   }
   // check if ip is blank
   if (getRoleIPTF().getText() == null ||
      getRoleIPTF().getText().equals("")) {
      msg.add("Please check IP Field - format ###.###.###.###");
   // check if ip is correct format
   else if (!getRoleIPTF().getText().matches(
         "([\\d]{1,3}\\.){3}[\\d]{1,3}")) {
      msg.add("IP Field Incorrect - format ###.###.###.###");
   }
   // dynamic dialog
   if (!msg.isEmpty()) {
      JOptionPane.showMessageDialog(this, msg.toArray());
      return false;
   }
   return true;
}

public void actionPerformed(ActionEvent e) {
   if (checkFields()){
      // all ok do something
      JOptionPane.showMessageDialog(this, "All fields are ok!");
      parent.next();
   }
}




Page 1 of 2



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel