CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Events

Events
Validate Swing input data
Display error messages

An error message displayed in a JOptionPane dialog box

The showMessageDialog method of the JOptionPane class

Syntax
showMessageDialog(parentComponent, messageString, titleString, messageTypeInt);

Arguments

Argument Description
parentComponent An object representing the component that's the parent of the dialog box.
messageString A string representing the message to be displayed in the dialog box.
titleString A string representing the title of the dialog box.
messageTypeInt An int that indicates the type of icon that will be used for the dialog box.

Fields used for the message type parameter

How to display error messages

  • The showMessageDialog method is a static method of the JOptionPane class that is commonly used to display dialog boxes with error messages for data validation.
  • You can also use the JOptionPane class to accept input from the user.

Code that displays the Invalid Entry dialog box

String message = "Monthly Investment is a required field.\n"
    + "Please re-enter.";
JOptionPane.showMessageDialog(this, // assumes "this" is a component
    message, "Invalid Entry",
    JOptionPane.ERROR_MESSAGE);
Previous | Display error messages | Text field | SwingValidator class | Validate multiple entries | Next