Over a million developers have joined DZone.
Platinum Partner

How to Display Rich Messages in JOptionPanes

· Java Zone

The Java Zone is brought to you in partnership with AppDynamics. Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems.

Are your JOptionPanes dull and uninspiring? Why not let them live up to their full potential and make the user's day more interesting!

I have been learning techniques for showing images and multiline texts in tooltips (here and here). The same techniques can be applied to JOptionPanes and JEditorPanes, for showing rich messages. Here is an example:

import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class NetBeansDay2008 {
public static void main(String[] args) {
       // Turn anti-aliasing on
System.setProperty("awt.useSystemAAFontSettings", "on");
final JEditorPane editorPane = new JEditorPane();

// Enable use of custom set fonts
editorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
editorPane.setFont(new Font("Arial", Font.BOLD, 13));

final JEditorPane editorPane = new JEditorPane();
editorPane.setPreferredSize(new Dimension(520,180));
+ "<body>"
+ "<table border='0px' cxellpadding='10px' height='100%'>"
+ "<tr>"
+ "<td valign='center'>"
+ "<img src='"
+ "http://www.netbeans.org/images/javaone/2008/nbday08_logo.gif"
// Alternatively an image stored next to the NetBeansDay2008
// class can be used like this.
//+ NetBeansDay2008.class.getResource("nbday08_logo.gif").toExternalForm()
+ "'>"
+ "</td>"
+ "<td>"
+ "<a href="http://www.netbeans.org/community/articles/javaone/2008/nb-day.html"><b>NetBeans Day</b></a>"
+ "San Francisco May 5th, 2008"
+ "NetBeans - The only IDE you need!"
+ "</td>"
+ "</tr>"
+ "</table>"
+ "</body>"
+ "</html>"

// TIP: Make the JOptionPane resizable using the HierarchyListener
editorPane.addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
Window window = SwingUtilities.getWindowAncestor(editorPane);
if (window instanceof Dialog) {
Dialog dialog = (Dialog)window;
if (!dialog.isResizable()) {

// TIP: Add Hyperlink listener to process hyperlinks
editorPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(final HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
EventQueue.invokeLater(new Runnable() {
public void run() {
// TIP: Show hand cursor
// TIP: Show URL as the tooltip
} else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
EventQueue.invokeLater(new Runnable() {
public void run() {
// Show default cursor

// Reset tooltip
} else if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
// TIP: Starting with JDK6 you can show the URL in desktop browser
if (Desktop.isDesktopSupported()) {
try {
} catch (Exception ex) {
Logger.getLogger(NetBeansDay2008.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
//System.out.println("Go to URL: " + e.getURL());
new JScrollPane(editorPane),
"NetBeans Day 2008",

I have incorporated the suggestion on anti-aliasing and custom set font and now it shows the following visually pleasing dialog:

NetBeans Day 2008, San Francisco May 5th 2008

See you there!

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}