JavaFX Magic 8 Ball

I was working on a Magic 8 ball written in JavaFX just to try a few things out, and people asked for the code, so here it is. It is nothing special, although I did try to comment the relevant code to make it a bit easier to understand. At around 150 lines of code (including whitespace and comments), it is a very small program, and a good example of what is possible with JavaFX.

To shake the magic 8 ball, you do that – you click on it and shake the ball around. Once you let go the message is updated.

Part of the reason I’ve posted this is so that people can tell me how to improve my code. If I’ve done some stupid (likely derived from my life as a Java developer), please let me know.

You can (hopefully) run the Java Web Start version of it here:

A screenshot of the program is shown here:

When the ball is shaken, the text and the triangle fade out, and then the triangle fades in with a new message.

A source code download (along with the background image), is available from here. I know I could have coded the ball in JavaFX, but frankly using an image is far easier 🙂

Finally, here is the code. Apologies for the need for horizontal scrolling, but it is the best I can do without changing the theme of this site to be wider.

Java desktop links of the week, May 10

Hey everyone. This post is going out a bit early as I’m actually away at my wifes graduation from vet school. I apologise that I am not as up to date as could be – I promise to have a better post next week! Regardless, I hope you enjoy!


  • Alexander Potochkin posted a short blog post updating people on the state of Swing tech in JDK 7 . In short, Nimbus (as we know) has been merged, and JXLayer is likely to be merged shortly. I have made extensive use of JXLayer in my projects, and highly recommend it. Some examples of JXLayer code can be found here .
  • Talking about Java 7, Kirill Grouchnikov released a video showing an animated JFrame which makes use of the Window.setOpacity() API which is new to Java 7.
  • Alex Ruiz posted announcing the release of FEST-Swing 1.2a1 .FEST-Swing is a Java library that provides a fluent interface for functional Swing GUI testing. This library provides an easy-to-use API that makes creation and maintenance of GUI tests easy. Version 1.2a1 is the first out of three alpha and three beta releases planned for version 1.2. This new version focuses on new features and improvements.
  • Christophe emailed me to let me know of BeanTableModel , which is a dedicated  binding solution (bi-directionnal) between POJO and a TableModel, where all configuration is done by java annotations.
  • I was looking for a Swing-based system tray library this week, as the API made available in Java 6 is AWT-based, and looks ugly. After much searching, the best code I could find was from the fishFarm project, and is an extension of other similar projects to try and remove various operating system quirks. You can find the single class, called JPopupTrayIcon, here .
  • Maxim Zakharenkov emailed me to let me know that Swing Explorer 1.4 has been released. As always, It can be found on the downloads page.

J avaFX

That’s it for the week. Keep up the hard work 🙂

Java Desktop links of the week, March 9th

This week there is a rather large amount of JavaFX news, but very little Swing news. I guess this is a sign of the times 🙂


  • Coming into my feed reader after posting this blog, I thought I’d quickly add that Ken Orr has a blog post about creating a custom HUD-style combo box.
  • Alexander Potochkin has blogged that the Swing Application Framework is back again. For more background, check out this interview with Hans Muller, back when the project was first approved. SAF has had a turbulent life, most recently with Karsten Lentzsch disapproving of its current state, and whether or not it actually will make it into Java 7 is yet to be seen.
  • InformIT has a post discussing playing media in Java using JMC. All code samples are written in Java, not JavaFX Script.
  • Regarding Swing 2.0, in the 13/02/09 podcast of This Ain’t Your Dad’s Java!, Sun’s Java marketing team has briefly discussed the recent discussion around Swing 2.0. You can listen to the podcast here if you have iTunes (skip forward to around the 5:00 mark. It continues until around the 9:15 minute mark). Their general perspective was that people wanting Swing 2.0 are trolling, whiney and fanboys. Also, despite their protests that Swing is strong, they inferred a number of  times that Swing  is on its way out. I found their means of communication to be somewhat condescending and rude, but perhaps that’s just me. For marketing people who are putting out the opinion of Sun, I found this to be disappointing.