TIP: Displaying rich messages using JOptionPane and JEditorPane

In this and this blog entry I discussed technics to show images and multiline text in tooltips. The same technics can be applied to show rich messages using JEditorPane. 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));

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><br>"
+ "San Francisco May 5th, 2008<br>"
+ "<br>"
+ "NetBeans - The only IDE you need!<br>"
+ "</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);
}
}

Based on a tip from a reader I have added the code to turn on anti-aliasing and use of custom set fonts in JEditorPane. This shows the following visually pleasing dialog:

NetBeans Day 2008, San Francisco May 5th 2008

See you there!

Comments:

Post a Comment:
Comments are closed for this entry.
About

sandipchitale

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today