Wednesday Jul 25, 2012

How to Get Started (FAST!) With JavaFX 2 and Scene Builder

A question I hear all too often is "How do I get started with JavaFX and/or Scene Builder?" Like most tools/toolsets, there are as many ways to use these implements as there are developers...but shouldn't there be an easy way to "hit the ground running"? While casting about for an end-to-end example, demo, or video, I found a wealth of useful information, but I couldn't find anything that took someone from start to finish, quickly and easily. JavaFX is easy and fun. But don't take my word for it; let's give it a try!

Before we begin, you'll need to visit this page to download and install all the necessary goodies. Click the download link to go to the current downloads page, then download and install JavaFX (NOTE: if you already have a JDK installed that includes JavaFX, you can omit this step), then download and install the JavaFX Scene Builder. Specific instructions for those steps are located on that page. Go ahead, I'll wait right here.  :-)

Back now? Great! Let's get started!

First, let's fire up Scene Builder just to make sure it works. If not, you'll want to revisit the installation instructions on the downloads page. Once that's working, we're ready to roll...assuming you're running NetBeans 7.2 or higher, of course. You are running NetBeans, right? If not, you owe it to yourself to download it - it's free, and as you're about to see, it's pretty amazing. NetBeans and Scene Builder are loosely-but-effectively integrated, and using both greatly simplifies things and makes your life easier.

Okay, here's the fun part: we're going to actually create a simple JavaFX application, create/modify a window using Scene Builder, and successfully test it in under 15 minutes. Don't believe me? Buckle up friend, here we go!


We start this fun frenzy with NetBeans. Choose File, New Project, JavaFX, then JavaFX FXML Application. This creates a very simple JavaFX app that includes a main application class, a controller class to provide the actual backing logic for the window defined in Scene Builder, and the FXML file containing our window definition (XML) code. I used the name "EasyJavaFX" for our project.

Here's a quick summary of these three files: contains the main application class. We won't really do anything with this class for this example, as its primary purpose in life is to load the window definition code contained in the FXML file and then show the main stage/scene. You'll keep the JavaFX terms straight with ease if you relate them to the theater: a platform holds a stage, which contains scenes. Simple. :-) is our controller class that provides the "brains" behind the graphical interface. If you open the SampleController, you'll see that it includes a property and a method tagged with @FXML. This tag enables the integration of the visual controls and elements you define using Scene Builder. Let's take a look at that next.

Sample.fxml is the definition file for your sample window. You can right-click and Edit the filename in the tree to view the underlying XML - and you may need to do that if you change filenames or properties by hand - or you can double-click on it to open it in Scene Builder. Let's do that next, but first a quick look at our little project:

Scene Builder

Opening Sample.fxml in Scene Builder results in the display of a very spartan window.

Let's rework it a little. For a complete tour of Scene Builder, please visit this page, but here's the nickel tour: stuff and navigation to the left, picture in the middle, and properties on the right. :-) We'll make some small modifications like so:

First, we click on the Button in the Hierarchy panel (bottom left), which selects it in the middle Content panel. We'll move it down and over a bit to make room for another button.

Next, let's drag another button from the Library panel and drop it onto the canvas, lining it up with the other button using the red positioning lines that appear while dragging it.

Once the button is positioned, we turn our attention to the Properties panel. We'll assign our new button an fx:id of exitButton, change the text to Exit, and tab out of the field to see our changes dynamically applied. Next, we click on the other button to change its fx:id and text to clickmeButton and "Click Me for an Important Announcement", respectively. Finally, we click the Exit button and resize it to be a bit wider. Who likes to hunt for tiny Exit buttons?

We're nearly done with our first round with Scene Builder. To finish, we select the Label in the Hierarchy panel at the bottom left - that's often the quickest way to locate "hidden" visual controls - and then resize it using the sizing handles on the canvas, again using the red lines to line up with edges and buttons. Once we're done, things should look something like this:

Click File, Save to save our changes, and Scene Builder confirms with a brief message at the top of the Content panel. Leaving Scene Builder open for convenience, return to NetBeans for the next step.

Back to NetBeans

Let's make a few changes to the controller class. Opening, let's start with the only method we (currently) have. Since we now have two buttons, we will need to keep two methods straight. Renaming handleButtonAction to something like handleClickmeButtonAction is a good start. And to add something of significance to read when the button is clicked, we'll replace the wonderfully traditional "Hello World!" with "Space... the Final Frontier. These are the voyages of the starship Enterprise. Its continuing mission: to explore strange new worlds, to seek out new life and new civilizations, to boldly go where no one has gone before." For those reading this who aren't Star Trek fans, please feel free to substitute your own, slightly less-interesting text.  :-)

Next, we'll create a method for our Exit button by either copying the other method and renaming it or hand-jamming it. Either way, be sure you have the @FXML tag to allow for integration with our window definition/Scene Builder. Putting the following line in our new handleExitButtonAction method will give us a quick escape hatch for our demo app:


To fix the error shown by NetBeans, click on the light bulb to the left of the code line and allow it to import javafx.application.Platform. After a quick click on the Save All button, we're good to go from the NetBeans side. Now to go back to Scene Builder to wrap things up.

And Back to Scene Builder

In order for our buttons to tie to our new methods, we need to connect them to the names we gave those methods in our SampleController class. First, we'll select the top Button (clickmeButton) in the Hierarchy panel. Next we turn our attention to the Properties panel on the right. At the bottom of the right side is a collapsed section labeled "Code: Button". Clicking that line opens the Code properties window so we can update the OnAction property. Notice it still points to the method formerly known as handleButtonAction. Click the dropdown, select handleClickmeButtonAction, tab out of the field, and we're done with that button.

We repeat some of the same steps with the other (Exit) button, although since we already have the Code properties panel open, selecting the button takes us directly there. Choosing handleExitButtonAction from its OnAction dropdown and tabbing out of the field concludes our work with Exit. But there is one more thing, purely cosmetic though it may be...

Since we added quite a lot of text to our label (see the handleClickmeButton method in the SampleController class), we may want to change the default behavior of our display label. Labels default to using an ellipsis when the length of text to display exceeds the space available, but we want to see the text in its entirety! Clicking the "Wrap" checkbox in the label's Properties panel fixes that up nicely and concludes our work in Scene Builder. Click File, Save, and then back to NetBeans for our maiden voyage!

And Now Back to NetBeans for the Big Finale!

Right-clicking the project in the Projects window to the left and clicking Run provides these satisfying results:

Clicking the "Click Me" button displays the following:

And clicking the Exit button closes the application.

Start to finish, you just developed a JavaFX application using Scene Builder in less time than it takes to drink a cup of coffee...while learning your way around in the process. Fast, fun, and productive: THAT is JavaFX.

All the best,

P.S. - There isn't much to the code, but I'll post it to GitHub if anyone wants it. Just let me know.
P.P.S. - If there is any interest in a video, please let me know that as well by commenting below. No promises, but if enough people ask and I can find some free time...

Thursday Jul 19, 2012

NetBeans (team) Responsiveness

When was the last time logging a potential bug/issue for any piece of software, with any vendor, was a pleasure? Most of us rank that with paying taxes, root canals, and visiting in-laws. :-)

Not so with NetBeans. I logged a possible bug report one morning...and by the next morning, it had been assigned, analyzed, and an explanation and workaround posted while code changes and testing take place. In fewer than 24 hours, it effectively went from start to finish from an end-user developer perspective. That may not always be possible, but it speaks very well for the level of commitment of the NetBeans team.

In addition to all the other things I appreciate about NetBeans, such as GlassFish integration/management, superb code generation, polished look-and-feel, ease of navigation, and many other things large and small, add excellent community support/dev engagement to the list. After using other IDEs for years, I could hardly be more pleased.

Those who haven't logged many tool issues may not fully understand this post, but it can be a big deal...and usually at the most inopportune times...  :-)

All the best,

Don't take my word for it, get your own! Visit the NetBeans site to download and check it out for yourself.

(Disclaimer: I work for Oracle, but not on the NetBeans team. I'm just another (very) satisfied user.)

Monday Jun 11, 2012

JavaOne 2012 Java Jungle Session!

Well, it's official - the proposal I submitted to JavaOne 2012 was accepted! Pending management approval, I'll be leading the following session:

Session ID: CON3519

Session Title: Building Hybrid Cloud Apps: Local Databases + The Cloud = Extreme Versatility

If you've been struggling with ways to "move to the cloud" without losing the advantages you currently enjoy/require in your current environment, I hope you'll consider signing up for this session.

Hope to see you there!


Tuesday Jun 05, 2012

Quick Fix for GlassFish/MySQL NoPasswordCredential Found

Just the other day, I stood up a GlassFish 3.1.2 server in preparation for a new web app we've developed. Since we're using MySQL as the back-end database, I configured it for MySQL (driver) and created the requisite JDBC resource and supporting connection pool. Pinging the finished pool returned a success, and all was well.

Until we fired up the app, that is -- in this case, after a weekend. Funny how things seem to break when you leave them alone for a couple of days. :-) Strangely, the error indicated "No PasswordCredential found". Time to re-check that pool.

All the usual properties and values were there (URL, driverClass, serverName, databaseName, portNumber, user, password) and were populated correctly. Yes, the password field, too. And it had pinged successfully. So why the problem?

A bit of searching online produced enough relevant material to offer promise. I didn't take notes as I was investigating the cause (note to self), but here were the general steps I took to resolve the issue:

First, per some guidance I had found, I tried resetting the password value to nothing (using () for a value). Of course, this didn't fix anything; the database account requires a password. And when I tried to put the value back, GlassFish politely refused. Hmm.

I'd seen that some folks created a new pool to replace the "broken" one, and while that did work for them, it seemed to simply side-step the issue. So I deleted the password property - which GlassFish allowed me to do - and restarted the domain. Once I was back in, I re-added the password property and its value, saved it, and pinged...success! But now to the app for the litmus test.

The web app worked, and everything and everyone was now happy. Not bad for a Monday.  :-D

Hope this helps,

Monday May 07, 2012

Looking Through the Telescope From the Big End: Four Small Java Developments that Pack a Punch!

There are a LOT of exciting developments in Java, almost on a daily basis. Most of them make a big splash, and for good reason! But sometimes, it's the little things...

There have been a lot of small developments in Java that are pretty exciting, too. I'll pick out four of my favorites - each at a different level and/or stage of development or detail - and explain why they're a big deal. Let's get started!

Home automation

I had the privilege of watching Vinicius and Yara Senger give a presentation at Jfokus (available below at on Java EE and home automation. The Sengers developed the jHome platform and run it on GlassFish, controlling Arduino-based appliances in their home, office, and boat. I won't give away all the spoilers, and it isn't Java end-to-end...but the Sengers are demonstrating a compelling, fully open source (hardware and software!) package for Java working in harmony with even the tiniest of devices.


FIRST, or "For Inspiration and Recognition of Science and Technology", was founded by world-renowned inventor Dean Kamen. In his words:

"Our mission is to inspire young people to be science and technology leaders, by engaging them in exciting mentor-based programs that build science, engineering and technology skills, that inspire innovation, and that foster well-rounded life capabilities including self-confidence, communication, and leadership."

FIRST's vehicle for doing this is robotics. Teams of various ages use available technology components and their brains to develop and program robots to compete in some pretty tricky challenges. This isn't Robot Wars (no Real Steel destructive matches here), but it's just as engaging - for the teams and spectators alike.

The 2012 FIRST International Championship was held in St. Louis, MO, USA, and teams representing several countries participated. I was able to attend for awhile - with an event like this, you just can't see it all! - and chanced upon a team in the pit area with a homemade sign saying "We use JAVA!". I was directed to the 15 year-old programmer who proudly informed me that they use NetBeans and Java to program their robot. We had a nice conversation, and I was left with the realization that Java's original "embedded" mission is still being realized in ways that we don't often think about.

Raspberry Pi

Simon Ritter recently posted about his work with the Raspberry Pi and JavaFX 2. I had seen others relate their victories getting OpenJDK onto the little Pi, a small-but-capable computer the size of a deck of cards...but Simon (and those who provided assists in whatever form) took the bar and threw it up onto the roof. It won't win any supercomputer awards (well, not alone anyway!), but the Raspberry Pi's small size and low price now make a host of formerly-unrealistic uses possible. Even before much optimization is done, JavaFX 2 is already running. If you've ever migrated existing software (of any kind) to a new platform, you know that that first "clean" run can be elusive. I can't wait to see how far we can take this...just as I can't wait for my pre-ordered Pi to hurry up and get here!

Harmonization of Java ME, SE

Henrik Stahl was on the Java Spotlight podcast recently (link below) presenting a clear vision for the harmonization of Java Micro Edition (ME) and Standard Edition (SE). The devil is in the details, of course, but here's the good news:

1) There is a plan
2) The plan makes sense
3) The plan is being worked...diligently!

Without getting into those details or the challenges surrounding them (API synchronization, Jigsaw, etc.), a modern, consistent set of APIs and a modular architecture should excite Java developers wherever they may fall along the spectrum.

The Bottom Line

It's an exciting time to work with Java at any level, and that includes in the crevices where small and/or embedded devices often lie hidden from view. If you haven't already been involved in "Java in the small", check it out. And if you have a favorite I've missed, drop me a line or comment below! If you like it, chances are the rest of us will, too.  :-)

All the best,

free counters


The Java Jungle addresses topics from mobile to enterprise Java, tech news to techniques, and anything even remotely related. The goal is to help us all do our work better with Java, however we use it.

Your Java Jungle guide is Mark Heckler, an Oracle Senior Java/Middleware/Core Engineer with development experience in numerous environments. Mark's current work pursuits and passions all revolve around Java and leave little time to blog or tweet - but somehow, he finds time to do both anyway.

Mark lives with his very understanding wife & kids in the St. Louis, MO area.

Stay Connected


« July 2015