Open source Java projects: Balloontip for Java

Add balloon tips to your Swing GUIs

1 2 3 4 Page 4
Page 4 of 4

Problems and solutions

The code fragment in Listing 7 reveals two interesting things about balloon tips:

  • Because BalloonTip adds a balloon tip to a JFrame's, JDialog's, or JInternalFrame's layered pane, it requires sufficient screen space to avoid being clipped. This is the reason for pnl.setBorder (BorderFactory.createEmptyBorder (75, 0, 0, 0));.
  • When a balloon tip is added to a layered pane, it is immediately shown when the GUI is made visible. Because this is not appropriate when the GUI is first revealed, the balloon tip is hidden via the final bt.setVisible (false); call before the GUI is made visible.

The need to reserve extra GUI space so that a balloon tip is not clipped against its containing layered pane is problematic. Although you could address this problem by placing a logo and/or help instructions in the extra space, Figure 6 reveals a better solution.

Using an external layered pane protects your balloon tip from being clipped.
Figure 6. Using an external layered pane protects your balloon tip from being clipped.

Figure 6 reveals the output from a VerifyAge2 application. This application presents a balloon tip via an undecorated dialog box that is dynamically positioned over the text field. To see how this works, check out the VerifyAge2.java source code in this article's code archive.

What is an undecorated dialog box?
An undecorated dialog box has no title bar or border. The same is true for any undecorated window.

Figure 6 also reveals a second problem associated with balloon tips: part of the application's main window is hidden by the dialog. This lack of transparency is due to Java not supporting transparent windows (at least on XP). Fortunately, this problem can be easily fixed: the next article in this series shows how.

Note: Balloon tips and the system tray

You might be wondering if you can use the balloontip API with the System Tray API to display balloon tips in the vicinity of tray icons (see Figure 1). For this to happen, three problems need to be solved:

  • Java does not support transparent windows on platforms such as Windows XP -- you saw the result of this lack of support in Figure 6.
  • The balloontip API currently orients balloon tips to the right. Because platforms such as Windows XP place the system tray on the right side of the task bar, orienting a balloon tip to the right could result in part of the tip being clipped.
  • The System Tray API does not expose tray icon positions -- it does not even expose components that could be interrogated to obtain positions.

In the next article in this series, I'll introduce a solution to the first problem. Solving the second problem involves making changes to the balloontip API so that balloon tips are left-oriented. I have yet to find a solution to the third problem.

In conclusion

The balloontip project suffers from a couple of weaknesses. One weakness is the lack of documentation. Although one might argue that a six-method API does not need to be documented, I believe that good Javadoc would make the balloontip API easier to use -- you would not necessarily have to explore the source code to learn how to use this API.

Another weakness is the API's incompleteness. Although the balloontip API provides setter methods for establishing a balloon tip's text, icon, and icon-text gap, there are no equivalent getter methods that return these items. Methods to get/set the attached component (and other factory method arguments) might also be useful.

Overall, the expression "Good things come in small packages" is applicable to balloontip. This project's simplicity makes it fairly easy to enhance a Swing GUI to assist the user, as evidenced by the VerifyAge1 and VerifyAge2 applications. Also, the LGPL license allows commercial code to benefit from this project. Consider using balloontip for your next Java project involving a Swing GUI!

Addendum: A new version of balloontip

Shortly after completing this article, I discovered a new version of balloontip. This version is bundled in the BalloonTip_2007-12-30.zip archive and introduces the following new features:

  • A balloon tip can be displayed above or below and on either side of the component to which it is attached by using BalloonTip's new Alignment enumeration and enhanced createRoundedBalloonTip() and createEdgedBalloonTip() factory methods.
  • The location of the balloon tip's triangular anchor can be moved to the center or a corner of the attached component by using BalloonTip's new TriangleTipLocation enumeration and public void setTriangleTipLocation(TriangleTipLocation triangleTipLocation) method.
  • The width of the empty border surrounding the balloon tip's close button can be specified by using BalloonTip's new public void setCloseButtonBorderWidth(int top, int left, int bottom, int right) method. By default, an edged balloon tip leaves a one-pixel border around its close button.
  • The balloontip API's class files are bundled in a balloontip_2007-12-30.jar file. Furthermore, the updated BalloonTipTestDrive example application's class files are also bundled in this JAR file. You can easily run the example via java -jar balloontip_2007-12-30.jar.

Because BalloonTip uses the enumeration language feature introduced by J2SE 5.0, it can no longer be used with earlier versions of Java. If this is a problem, you need to change the source code.

Jeff Friesen is a freelance software developer and educator who specializes in Java technology. Check out his javajeff.mb.ca website to discover all of his published Java articles and more.

Learn more about this topic

1 2 3 4 Page 4
Page 4 of 4