Sunday 23 October 2011

Java Message Box (Alert Popup)

A simple alternative to printing out messages in the console, this code will popup a message box in Java similar to that of Javascript alert("hello").

The code can be modified to include several types of message box types such as an error icon or different button options. Have a look JOptionPane in the JavaDoc's for more information on these.

import javax.swing.JOptionPane;

/**
 * Displays a popup message box in Java
 */
public class MessageBox
{

  /**
   * Displays a popup message box with the Information Message icon and button "Okay"
   * 
   * @param message the message to display in the popup
   */
   public static void MessageBoxOkayInfo(String message)
   {
     JOptionPane.showMessageDialog(null, message, "Alert",
     JOptionPane.INFORMATION_MESSAGE);
   }
}

To try this code use this snippet.
MessageBox.MessageBoxOkayInfo("Hello World!");

No comments:

Post a Comment