JOptionPane Display Message Swing Example

March 28, 2013 | Swing
JOptionPaneSwing is used to display message to the user in a window (separate dialog box). It is the easiest way of displaying message in swing. This can be used in non-gui programs also.
Example on JOptionPane using showMessageDialog() method
import javax.swing.*;
public class JOptionPaneDemo
  public static void main(String args[])
    String str = "SN Rao Greets You Sir";
    JOptionPane.showMessageDialog(null, str, "Learning to Display", JOptionPane.INFORMATION_MESSAGE);	

[caption id="attachment_13084" align="aligncenter" width="268"]JOptionPane Displaying message with[/caption]

In the above code:

null : to connect to a help file, if exists. str : message to display Learning to Display : Title to the dialog box JOptionPane.INFORMATION_MESSAGE : to get an icon on tahe dialog box

Other messages that can be called with JOption are:


They differ in the icon displayed on the dialog box. Observe the i icon within a circle on the dialog box for INFORMATION_MESSAGE. . Note: It is advised to read Java JFC Swing Introduction and Swing Overview before reading this.

There us way to take input from the user also with the same JOptionPane. Refer Swing JOptionPane - Taking User Input

On JLabel, total 3 programs exist with different properties set. 1. Swing JLabel - ToolTip and Icon 2. JLabel Border and Image 3. JLabel Multiline Text