O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Create Custom Tool Tips #43
Chapter 6, Transparent and Animated Windows
|
225
HACK
H A C K
#43
Create Custom Tool Tips
Hack #43
Replace the standard rollover tool tip with an attractive custom version,
including a border and rounded corners.
Every Swing component can have a tool tip, a little snippet of explanatory
text that pops up when you let your mouse cursor linger over the compo-
nent. These tool tips are often useful, but they usually look quite boring.
This hack shows how to create visually interesting tool tips with a custom
subclass.
In Swing, all tool tips are instances of the
JToolTip class. To create your own
version, you need only subclass
JToolTip and override the paintComponent( )
method. In this hack, we’ll create a tool tip with a rectangle that has a bev-
eled border and a white background. The actual drawing can be taken care
of with a few Java2D drawing commands. Example 6-4 is the code to draw
the tool tip’s background and border.
Example 6-4. A nice-looking tool tip
class CustomToolTip extends JToolTip {
public void paintComponent(Graphics g) {
// create a round rectangle
Shape round = new RoundRectangle2D.Float(4,4,
this.getWidth( )-1-8,
this.getHeight( )-1-8,
15,15);
// draw the white background
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.white);
g2.fill(round);
// draw the gray border
g2.setColor(Color.gray);
g2.setStroke(new BasicStroke(5));
g2.draw(round);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_DEFAULT);
// draw the text
String text = this.getComponent().getToolTipText( );
if(text != null) {
FontMetrics fm = g2.getFontMetrics( );
int h = fm.getAscent( );
226
|
Chapter 6, Transparent and Animated Windows
#43 Create Custom Tool Tips
HACK
This code creates a round rectangle shape that is then reused to draw the
background and border. Notice that anti-aliasing is turned on for drawing
the shape, but it’s turned back to the default (which could be on or off)
before drawing the text. It would look strange to have anti-aliased text if the
rest of the interface was still using standard aliased text—using the default is
a safer idea.
Because this tool tip needs extra space around the text to draw the border,
you will need to modify the tool tip’s preferred size. The
getPreferredSize( )
method here adds an extra 20 pixels in each direction:
public Dimension getPreferredSize( ) {
Dimension dim = super.getPreferredSize( );
return new Dimension((int)dim.getWidth( )+20,
(int)dim.getHeight( )+20);
}
The tool tip has rounded corners, but with the code as it stands, you would
still see the slivers of gray that fill out the component’s real corners. In order
to hide the gray and let the components below shine through, you need to
make the tool tip transparent. You can do this by setting
opaque to false in
the tool tip’s constructor. The tool tip component is not a direct child of the
frame that contains the tool tip. There is another component between the
tool tip and the frame that will be visible even if the tool tip is transparent.
You can make this extra component transparent with an additional
setOpaque(false) call on the tool tip’s parent at the start of the
paintComponent( ) method:
public CustomToolTip( ) {
super( );
// make the tool tip not fill in its background
this.setOpaque(false);
}
public void paintComponent(Graphics g) {
// set the parent to not be opaque
Component parent = this.getParent( );
if(parent != null) {
if(parent instanceof JComponent) {
JComponent jparent = (JComponent)parent;
if(jparent.isOpaque( )) {
g2.setColor(Color.black);
g2.drawString(text,10,(this.getHeight( )+h)/2);
}
}
Example 6-4. A nice-looking tool tip (continued)

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required