JOptionPane Display Message Swing Example

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);	
  }
}
JOptionPane
Displaying message with JOptionPaneDemo.java

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:

INFORMATION_MESSAG, ERROR_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, PLAIN_MESSAGE

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

Leave a Comment

Your email address will not be published.