Automatically Update your Javapackager Applications

December 4, 2014

For years we shipped our desktop applications, ReportMill and SnapCode, as Java Web Start applications, enjoying the built-in auto-update feature it provided. This year we began shipping our apps as native app packages using Javapackager, which makes it easy to generate a exe/app for Windows/MacOS. This has been a huge improvement – we no longer worry what JRE is installed, whether it is broken/missing or whether it will change. The JRE is entirely under our control and hidden from the user. The fact that we use Java at all is now just an “implementation detail”.

The only concern we had when switching was that we still want users to automatically get the latest update without having to re-install the latest version. Fortunately, we found a simple solution: We put the core of the app in a single compact jar file (.pack.gz – like Web Start) on our website and we have an AppLoader main class that does the following:

  1. Check for new version of jar file on web site
  2. If found, download and alert user “New Update Available on Relaunch”
  3. If update from above step is in App-Support directory, move into place
  4. Create URLClassLoader for latest jar
  5. Get main class from URLClassLoader, and invoke real app main()

We were able to do this in a single file in 250 lines of code that only uses JRE classes. To use this in your application, add to your project, make it the main class and customize the string constants at the top of the file for your AppName, JarName, JarURL and MainClass.

Thanks to Jim Weaver for inspiring us to share this. Hopefully this works well for you, too!

Source code:


SnapCode Interactive Exercises

October 24, 2014

At JavaOne we presented a new feature of SnapCode we have been working on to provide interactive exercises to learn Java. The exercises come in the form of a puzzle game, which has the following parts:

  • Instructions for a task
  • A set of puzzle pieces to complete the task
  • A skeleton class to add the pieces to

The user is expected to drag in the puzzle pieces in the right order and run the app. When the task completes an automatic checker (unit test) grades you to see how well you did. The first exercise we have is called “Teach Sparky to Fetch”.


I’ve posted a short, updated SnapCode overview video that shows this in action (starting at the 4m 30s mark).

Video: SnapCode Overview with Exercises

SnapCode Debugger Improvements

August 9, 2014

I’ve spent the last few weeks boning up on the Java Debug Interface (JDI) API and improving SnapCode’s debugger. The debugger is now more accessible with a prominent “Debug” button in the Processes pane, easy viewing of debug threads and stack frames, and easy access to pause, continue, and step into/over/out of the current line of code.

Additionally, there is a new “Variables” tab to show the variables in the currently selected stack frame, and a “Debug Console” pane to allow you to type any valid Java expression and have it evaluated on the spot.

I still have a few debugger bugs to squash, and next week I need to change the Variables table to be a JavaFX TreeTable, but the debugger is very usable now. Here is a snapshot of the bottom part of the project window showing a process being debugged with the Variables pane selected in the Support tray.



SnapCode is the first and only pure JavaFX IDE

June 11, 2014

SnapCode is now the first and only pure JavaFX IDE! This allows SnapCode to easily implement page transitions, mouse over effects, animations and the SnapCode visual code editing that give SnapCode the feel of rich modern applications.

For the last two weeks we’ve been converting the remaining Swing parts of SnapCode to pure JavaFX. This includes the code editor, the runtime browser/player, the welcome panel and much more. We had some secret sauce in there that made the port happen remarkably quick.

This is just our first preview of the pure JavaFX world. We promise a balance of tasteful modern UI effects as we continue to polish SnapCode. But for now, we couldn’t resist some gratuitous effects that JavaFX makes so easy.

Check out the video overview and let us know what you think!

YouTube Video Overview: SnapCode is the First pure JavaFX IDE.



Getting the Visible Bounds of a Node

June 11, 2014

Today I set out to optimize a custom text node I have (for code editing) to only create nodes for lines of text that are visible – much like ListView only create cell nodes for visible items.

So the crux of this is that I needed a method similar to Swing’s JComponent.getVisibleRect() to tell me the visible bounds for my text node, which is usually in a ScrollPane. It seems there is currently no Node method for this and I couldn’t find previous work online, so I thought I would post my solution.

Here is a method that was sufficient for my purposes – it returns the visible bounds for a node based on the first ancestor clip (which could be from a ScrollPane, SplitPane or any Parent that clips its children):

 * Returns the visible bounds for a node based on first ancestor clip
 * (or null if no clipping found).
public static Bounds getVisibleBounds(Node aNode)
    // If node not visible, return empty bounds
    if(!aNode.isVisible()) return new BoundingBox(0,0,-1,-1);
    // If node has clip, return clip bounds in node coords
    if(aNode.getClip()!=null) return aNode.getClip().getBoundsInParent();
    // If node has parent, get parent visible bounds in node coords
    Bounds bounds = aNode.getParent()!=null? getVisibleBounds(aNode.getParent()) : null;
    if(bounds!=null && !bounds.isEmpty()) bounds = aNode.parentToLocal(bounds);
    return bounds;

I also wrote a version called getVisibleBoundsScene(node) which intersects the combined clips of every ancestor node all the way up to the scene. This version was only slightly more complicated, but it turned out to be overkill. In fact, if you have animations where panels are sliding in and out, of Parent containers, using the cumulative intersection of visible bounds can significantly slow things down.

So after I had my VisibleBounds solution, I had one more problem: if I only add child nodes for the visible bounds, I also need to rebuild when those visible bounds change. I achieved this by listening for bounds changes in ancestors up to the clip, a list that is updated every time the node is rebuilt. Below is the code I used for that.

 * Sets listeners for visible bounds.
void setVisibleBoundsListeners()
    // Get list of ancestors up to first clip (just return if same as last time)
    List pars = new ArrayList();
    for(Node p=this; p!=null && p.getClip()==null; p=p.getParent()) pars.add(p);
    if(pars.equals(_pars)) return;
    // Stop listening to old ancestors, start listening to new
    for(Node par : _pars) par.boundsInParentProperty().removeListener(_bcl);
    _pars = pars;
    for(Node par : _pars) par.boundsInParentProperty().addListener(_bcl);

// Listeners
List  _pars = new ArrayList();
ChangeListener _bcl = new ChangeListener() {
    public void changed(ObservableValue<? extends Bounds> observable, Bounds oldValue, Bounds newValue) {
        resetChildNodes(); }}; 

Make ScrollPane Content fill Viewport Bounds

June 3, 2014

One of the most common issues I find when using ScrollPanes in JavaFX is that I need the Content to fill the width/height when the ScrollPane.ViewportBounds is larger than the Content.PrefWidth/PrefHeight.

My first thought was that the ScrollPane properties FitToWidth and FitToHeight were meant to solve this – but they force the Content to ViewportBounds even when the Content has a larger PrefWidth/PrefHeight (this is probably useful for things like a wrapped text view). I suppose it should be possible to create a conditional binding for these properties, but I haven’t looked into this.

My second thought was that maybe I could call HBox.setHgrow(content, Priority.Always) and VBox.setVGrow(content, Priority.Always), but that was ineffective.

The solution I settled on was to add a ChangeListener to ScrollPane.ViewportBoundsProperty to reset ScrollPane.FitToWidth/FitToHeight dynamically:

// Create ScrollPane
final ScrollPane spane = new ScrollPane(aTextArea);

// Add listener to set ScrollPane FitToWidth/FitToHeight when viewport bounds changes
spane.viewportBoundsProperty().addListener(new ChangeListener() {
  public void changed(ObservableValue<? extends Bounds> arg0, Bounds arg1, Bounds arg2) {
      Node content = spane.getContent();

Please comment if you have a better solution.

SplitPane Animation with DoubleTransition

May 29, 2014

Today I added a nice effect to SnapCode which slides a SplitPane item onscreen when a button is clicked. This functionality was actually ported over from Swing where it required a significant amount of code, but in JavaFX, I was able to achieve the same effect with just a few lines of code.

So when I click on the “Add Support Pane” button, this code adds the SupportPane to the SplitPane and triggers the animation:

  DoubleProperty dprop = mySplitPn.getDividers().get(0).positionProperty();
  DoubleTransition dt = new DoubleTransition(Duration.millis(500), dprop);
  dt.setFromValue(1); dt.setToValue(500);;

When I click the button again (to remove), similar code is called, except that the item is removed OnFished:

  DoubleProperty dprop = mySplitPn.getDividers().get(0).positionProperty();
  DoubleTransition dt = new DoubleTransition(Duration.millis(500), dprop);
  dt.setOnFinished(new EventHandler<ActionEvent>() {
    public void handle(ActionEvent e) {
      getBrowserBox().getItems().remove(1); }});

So it is very simple to add such a nice animated effect. Now the only slight complication is that there is no “DoubleTransition” class in JavaFX. It turns out that this class is trivial to implement with just a slight modification to something like FadeTransition. And this DoubleTransition class will undoubtedly come in handy in other places as well.

Here is a link to the source:

And a link to a simple video demo as well: SplitPane Animation Video

SuperMario JavaFX in SnapCode

May 15, 2014

This week I flushed out more SnapCode JavaFX sprite functionality using a version of Super Mario written for Greenfoot. This required new SnapScene API to control the order of painting: setPaintOrder(classes) and some touch up to the hit detection code. Check out the two minute video below.

YouTube Video: SuperMario JavaFX in SnapCode


JavaFX PacMan in SnapCode with Greenfoot

May 6, 2014

This week I flushed out a set of classes to emulate Greenfoot in JavaFX and  SnapCode. My test case was a nice version of PacMan written by Dan Post. For this I had to add a number of drawing methods to SnapImage (which uses JavaFX Canvas) and I had to add support for playing MP3 sound files (which uses JavaFX Media and MediaPlayer).

Check out the screenshot and 3 minute video of the game in action with some custom/silly JavaFX modding thrown in for fun.

YouTube Video: JavaFX PacMan in SnapCode with Greenfoot


SnapCode Update April 25: Added SnapImage

April 27, 2014

This week I added a class to SnapCode to facilitate “sprite” programming with SnapActor. The class is called SnapImage and it has methods to load an image from a file name, and methods to do all kinds of drawing and basic image manipulation: flipImage, rotateImage, sizeImage, setColor (for a pixel), etc.

SnapActor now has a primary attribute that is a SnapImage, so you can easily create actors with custom drawing and you can easily change the look of an actor on the fly by resetting or manipulating the SnapActor.Image.

Additionally, I added a new SnapActor subclass to make it possible to have compound or nested actors. This class is called SnapParent, and it basically has a list attribute called Actors that lets you add/remove child actors, and it has methods to do hit detection on those child actors and/or retrieve subsets of the actors by class. Since this essentially what SnapScene does, I made SnapScene a SnapParent subclass to get more consistency and code re-use.

Here is a full list of the week’s changes:

– SnapImage: Added class to add rendering and drawing functionality to Actors
– SnapImage: Added flipImageX/flipImageY/rotateImage() to get current image in new orientation
– SnapImage: Added getColor(int,int) and setColor(int,int,color)
– SnapImage: Added Opacity attribute
– SnapImage: Added fillRoundRect, strokeLine, strokeRect, strokeRoundRect
– SnapImage: Added drawImage(image, x,y)
– SnapScene: Added start/stop methods
– SnapParent: Created Actor subclass that can have child actors
– SnapScene: Made this a subclass of SnapParent so it gets same Actor/Image management

SnapCode is available here.