Sandip has posted 5 posts at DZone. View Full User Profile

How to Display Rich Messages in JOptionPanes

  • submit to reddit

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='"
+ ""
// 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=""><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!

zany.jpeg2.66 KB
Your rating: None
Published at DZone with permission of its author, Sandip Chitale.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Mike P(Okidoky) replied on Mon, 2008/04/14 - 1:32pm

Ok, now change the font, anti-alias it, and integrate the OK button in the html area.


Andrew McVeigh replied on Mon, 2008/04/14 - 3:20pm in response to: Mike P(Okidoky)

Ok, now change the font, anti-alias it, and integrate the OK button in the html area.


Am I missing something? I do the above in my own software all the time. To anti-alias, use something like:

   System.setProperty("awt.useSystemAAFontSettings", "on"); 

This will force it to on. Leave this unset to pick up the system anti-alias settings ("lcd" in my case).

For the font, simply ask the editor pane to honor the display settings:

  editorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
editorPane.setFont(new Font("Arial", Font.PLAIN, 36));

The above works nicely for me...

And for the button, simply put in form links into the html:

<body><FORM METHOD='LINK' ACTION='test'>
<input type='submit' value='OK'>...

Then add a listener to catch any events:

  editorPane.addHyperlinkListener(new HyperlinkListener()
public void hyperlinkUpdate(HyperlinkEvent e)
{ ...

You can even embed image buttons and catch their events.

If this isn't what you meant, perhaps you could clarify?





Sandip Chitale replied on Tue, 2008/04/15 - 7:29pm


Thanks for your tips about:








Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.