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

How to Display Rich Messages in JOptionPanes

04.14.2008
| 8625 views |
  • 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));
editorPane.setEditable(false);
editorPane.setContentType("text/html");
editorPane.setText(
"<html>"
+ "<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()) {
dialog.setResizable(true);
}
}
}
});

// 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
SwingUtilities.getWindowAncestor(editorPane).setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
// TIP: Show URL as the tooltip
editorPane.setToolTipText(e.getURL().toExternalForm());
}
});
} else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
EventQueue.invokeLater(new Runnable() {
public void run() {
// Show default cursor
SwingUtilities.getWindowAncestor(editorPane).setCursor(Cursor.getDefaultCursor());

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

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!

AttachmentSize
zany.jpeg2.66 KB
0
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.)

Comments

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?

Cheers,

Andrew

 

 

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

Andrew,

Thanks for your tips about:

awt.useSystemAAFontSettings

and

JEditorPane.HONOR_DISPLAY_PROPERTIES.

Cheers,

Sandip

 

 

Comment viewing options

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