Tuesday Feb 26, 2013

How to "Pretty Up" Your JavaFX TableViews

One day you're a happy JavaFX developer, cranking out applications and blissfully providing your application's users with table after table of rich textual information. The next day it happens: your users approach you and ask if there's a way to include a thumbnail of each part in the inventory list. Or a small photo in the contact list/personnel roster. Or a snapshot/graph of the latest conditions for each monitored sensor or weather camera. So now what?

PropertyValueFactory, a convenience implementation of the Callback interface, is typically used to handle the work involved with populating all cells within a single TableColumn. As long as you provide the name of a valid property to the PropertyValueFactory constructor and set the TableColumn's CellValueFactory to the resulting PropertyValueFactory, the cell is populated, TableView adds an observer to the value, and life moves merrily along.

Adding a graphical element to a TableView isn't much harder, actually. The TableColumn's CellFactory handles rendering of the cell's contents, so if you want to place something other than textual content in the cell, you'll need to also set the TableColumn's CellFactory to one of your liking and override the updateItem() method. Let's take a look at a quick example.

I'd always intended to create some small-but-useful Twitter utilities and hadn't really had the opportunity, but a simple Twitter client provides a perfect demonstration of this capability. Here is an example of what a "feed" interface might look like:


For this TableView example, there are only two columns:

  • one for the user, showing the user's picture, name, and screen name
  • one for the tweet

The tweet's text is simply a string property of each MTweet object, but the user information comes from several properties of the MTweet object's associated MUser object. The MTweet's text is properly assigned to the cell's text property per the discussion above, but what about the user information? Per the JavaFX 2.2 docs,

Because by far the most common use case for cells is to show text to a user, this use case is specially optimized for within Cell. This is done by Cell extending from Labeled. This means that subclasses of Cell need only set the text property, rather than create a separate Label and set that within the Cell. However, for situations where something more than just plain text is called for, it is possible to place any Node in the Cell graphic property. Despite the term, a graphic can be any Node, and will be fully interactive. For example, a ListCell might be configured with a Button as its graphic. The Button text could then be bound to the cells item property. In this way, whenever the item in the Cell changes, the Button text is automatically updated.

To display the tweet's text in the "Text" column of our table, a couple lines of code does all that is necessary:



In order to bundle the user information and place it within a single cell, we set the "User Info" column's CellValueFactory in similar fashion to our text column above:



And then we set that column's CellFactory to something along the order of this:



For each cell in that TableColumn, updateItem is called with that cell's contents as the "item", the first parameter. Within our example above, we create a VBox, populate it with the user's photo (Image, via ImageView), name (String) and screen name (String), do a bit of formatting, and assign the VBox to the cell's graphic property. And with that, we're off and running. :-)

There is much more you can do, and this should get the ideas flowing. Here's to prettier apps (better optics?) and happier users!

All the best,
Mark

<<< UPDATE 1 >>>

Jonathan Giles, JavaFX team UI Controls tech lead, made a few suggestions for improving the efficiency of the code used above. The points he made were excellent, and I wanted to provide an update that shows revised code that incorporates them. I did leave the original code above, as I'm hoping it helps make a clearer path for those implementing similar code for the first time.

In order to override the TableCell constructor, we'll extend the particular TableCell<MTweet, MUser> class. Doing so allows us to create the VBox, Labels, and ImageView used in our composite user cell once per cell, rather than each time updateItem() is called. While we're at it, we'll also perform formatting/assignment duties in the constructor and avoid repeating those calls. Here is the new derivative class:

Creating the new UserTableCell class means we can update our setCellFactory() method call for our user info column like so:

Thanks to Jonathan Giles (@JonathanGiles) for the suggestions!

All the best,
Mark

<<< UPDATE 2 >>>

One more update! This time using an anonymous inner class for the column's cell factory. Same basic functionality, but this is perhaps the tidiest option.


Options are good, as they say...  :-)

All the best,
Mark

Monday Jan 14, 2013

Secure Email from Java

I've been working recently with a client to do some rather useful things with notifications, and one of them involved sending a secure email from within a Java program. We encountered some interesting (translation: weird!) challenges, and in overcoming them, I worked out a reasonably straightforward path through the minefield. If you've been thinking about secure-email-enabling your Java app but aren't sure where to start, hopefully this will serve as a fairly quick and mostly painless primer.  :-)

The Problem

Let me first say that if you only want to send a plain-text email from Java, there are ways to do that without much fuss and without any external players. If you want to sign or encrypt your emails, though, you'll need a couple of extra components:
1. a digital certificate (private/public key pair issued by a recognized Certificate Authority, or "CA") and
2. a means of using the certificate to sign and/or encrypt the email

The goal is to digitally sign an email to assure recipients that the sender of the mail is indeed me (or you, if you're following along at work/home). Let's get started!

Getting Your Tools in Order

Getting a Certificate

First, we have to have a digital certificate. If you already have one, you can skip this step...but if not, StartSSL offers free user/email certificates for personal use. Just point your browser to the StartSSL site and click the large button labeled "Sign-up". You'll need to provide them some information, enter the verification code they email to the address you provide, and they do the rest...including installing your new cert and the certificate chain into your web browser.

Freeing the Certificate from your Browser

Perhaps the easiest way to use a certificate is to store it (keys, certificate chain) in a Java Keystore (jks). Extracting your shiny new certificate from your browser is a relatively easy (albeit drawn-out) process. From your browser, export your certificate, including private key. This will produce a .pfx file, which is a PKCS12 keystore. From Chrome, you simply:
  1. Click on the Wrench (or Lines) icon in the upper-right corner
  2. Select "Settings" from the menu
  3. "Show advanced settings..." at the bottom of the page
  4. Scroll down to the section labeled "HTTPS/SSL"
  5. Click the "Manage certificates..." button to display your certificates.
Then, from the certificates window:
  1. Select the target certificate and click the "Export..." button
  2. Click "Next" from the Export Wizard window
  3. Choose "Yes, export the private key" and click "Next"
  4. Under the "Personal Information Exchange - PKCS #12 (.PFX)" entry, select the options to "Include all certificates in the certification path if possible" and "Export all extended properties" (NOTE: Do NOT choose to "Delete the private key if the export is successful". No no no!) and click "Next"
  5. Enter a password (twice) and click "Next"
  6. Provide a path/filename for the export and click "Next", and finally...
  7. Confirm the export options and click "Finish".
Now that we've liberated your cert from the browser, let's make it usable by our (non-browser) Java program.

Creating a Java Keystore

The fastest, easiest way I've found to convert a .pfx file to a .jks (Java Keystore) file is with the Oracle 11g database client. The database client can be downloaded by following this link, selecting the "See All" link to the right of your listed operating system, and choosing the 11g client from the OS-specific download page. Once it's downloaded and installed, you're ready to proceed.

Like the .pfx file, the Oracle wallet is a PKCS12 keystore, and the orapki utility (and other tools) included with the database client can be used to manipulate it...as long as it thinks it's dealing with an Oracle wallet. To make that happen, simply rename the .pfx file to ewallet.p12. Since we aren't dealing with the Oracle Wallet Manager, we don't have to worry about meeting OWM's password criteria or other niceties. Yes, that really is all there is to it!

Now, to make a Java Keystore. To do that, you'll need to open a command prompt and do the following tasks:
  1. Create an ORACLE_HOME environment variable that points to the install location of the Oracle client
  2. Run the following command, pointing to the orapki utility under %ORACLE_HOME%\bin (in Windows) or $ORACLE_HOME/bin (Mac/Linux/UNIX):
orapki wallet pkcs12_to_jks -wallet <wallet_directory> -pwd <wallet_password> -jksKeyStoreLoc <java_key_store_path_and_filename> -jksKeyStorepwd <jks_password>

You should now have a brand new Java KeyStore! You can verify its contents with the OpenSSL keytool utility:
keytool -list -keystore <java_key_store>

Now that we have our credentials in order, on to the Java side of things!

Building the Solution

There are several Java libraries available that aid in signing and/or encrypting email. Of the non-commercial options I found, all use the Bouncy Castle Crypto API and libraries as their underpinnings. Bouncy Castle (BC) may have a funny name, but it's all business with regard to encryption.

At a very high level, you need to do the following things to create/send a signed email:
  1. Provide the email "essentials": SMTP server host & port, email addresses (sender & receiver), a subject, content, and the sending user's password
  2. Add BC as a new crypto provider
  3. Retrieve the cert from your Java Keystore
  4. Create and sign the email using the BC API/libraries
  5. Send the email
There is much more you can do of course, but these are the "must-haves".

In preparation for developing our secure email module, I created a proof of concept (BCCrypTool) by marrying a Java email program I'd written previously and some BC sample code...code reuse at its lowest level, but still good for the environment. :-) What you see here in my GitHub repo is a bit of streamlined Java code that should be pretty easy to repurpose. Please feel free to take a copy and do just that, and if you make significant changes/improvements and are able and inclined to share them, please feel free to do that as well. Sharing is caring.  :-)

A quick note on libraries. You'll need the following to make this work:
And from Bouncy Castle, the following:
  • The BC provider library (bcprov-jdk15on-147.jar)
  • The BC S/MIME library (bcmail-jdk15on-147.jar)
  • The BC security library (bcpkix-jdk15on-147.jar)
There are other BC libraries available here if you'd like to take things even further.

All the best to you in your Java secure email adventures!

Mark

Saturday Nov 10, 2012

Polishing the MonologFX API

Earlier this week, I released "into the wild" a new JavaFX 2.x dialog library, MonologFX, that incorporated some elements of DialogFX and new features I'd been working on over time. While I did try to get the API to a point of reasonable completion (nothing is ever truly "finished", of course!), there was one bit of functionality that I'd included without providing any real "polish": that of the button icons.

Good friend and fellow JFXtras teammate José Pereda Llamas suggested I fix that oversight and provide an update (thanks much, José!), thus this post. If you'd like to take a peek at the new streamlined syntax, I've updated the earlier post; please click here if you'd like to review it. If you want to give MonologFX a try, just point your browser to GitHub to download the updated code and/or .jar.

All the best,
Mark

Wednesday Nov 07, 2012

MonologFX: FLOSS JavaFX Dialogs for the Taking

(UPDATED Nov 10 with simpler button icon API) 

Some time back, I was searching for basic dialog functionality within JavaFX and came up empty. After finding a decent open-source offering on GitHub that almost fit the bill, I began using it...and immediately began thinking of ways to "do it differently."  :-)  Having a weekend to kill, I ended up creating DialogFX and releasing it on GitHub (hecklerm/DialogFX) for anyone who might find it useful. Shortly thereafter, it was incorporated into JFXtras (jfxtras.org) as well.

Today I'm sharing a different, more flexible and capable JavaFX dialog called MonologFX that I've been developing and refining over the past few months. The summary of its progression thus far is pretty well captured in the README.md file I posted with the project on GitHub:

After creating the DialogFX library for JavaFX, I received several suggestions and requests for additional or different functionality, some of which ran counter to the interfaces and/or intent of the DialogFX "way of doing things". Great ideas, but not completely compatible with the existing functionality. Wanting to incorporate these capabilities, I started over...incorporating some parts of DialogFX into the new MonologFX, as I called it, but taking it in a different direction when it seemed sensible to do so.

In the meantime, the OpenJFX team has released dialog code that will be refined and eventually incorporated into JavaFX and OpenJFX. Rather than just scrap the MonologFX code or hoard it, I'm releasing it here on GitHub with the hope that someone may find it useful, interesting, or entertaining. You may never need it, but regardless, MonologFX is there for the taking.

Things of Note

So, what are some features of MonologFX?

  • Four kinds of dialog boxes: ACCEPT (check mark icon), ERROR (red 'x'), INFO (blue "i"), and QUESTION (blue question mark)
  • Button alignment configurable by developer: LEFT, RIGHT, or CENTER
  • Skins/stylesheets support
  • Shortcut key/mnemonics support (Alt-<key>)
  • Ability to designate default (RETURN-key) and cancel (ESCAPE-key) buttons
  • Built-in button types and labels for OK, CANCEL, ABORT, RETRY, IGNORE, YES, and NO
  • Custom button types: CUSTOM1, CUSTOM2, CUSTOM3
  • Internationalization (i18n) built in. Currently, files are provided for English/US and Spanish/Spain locales; please share others and I'll add them!
  • Icon support for your buttons, with or without text labels
  • Fully Free/Libre Open Source Software (FLOSS), with latest source code & .jar always available at GitHub

Quick Usage Overview

Having an intense distaste for rough edges and gears flying when things break (!), I've tried to provide defaults for everything and "fail-safes" to avoid messy outcomes if some property isn't specified, etc. This also feeds the goal of making MonologFX as easy to use as possible, while retaining the library's full flexibility. Or at least that's the plan.  :-)

You can hand-assemble your buttons and dialogs, but I've also included Builder classes to help move that along as well. Here are a couple examples:

        MonologFXButton mlb = MonologFXButtonBuilder.create()
                .defaultButton(true)
                .icon("/testmonologfx/dialog_apply.png")
                .type(MonologFXButton.Type.OK)
                .build();

        MonologFXButton mlb2 = MonologFXButtonBuilder.create()
                .cancelButton(true)
                .icon("/testmonologfx/dialog_cancel.png")
                .type(MonologFXButton.Type.CANCEL)
                .build();

        MonologFX mono = MonologFXBuilder.create()
                .modal(true)
                .message("Welcome to MonologFX! Please feel free to try it out and share your thoughts.")
                .titleText("Important Announcement")
                .button(mlb)
                .button(mlb2)
                .buttonAlignment(MonologFX.ButtonAlignment.CENTER)
                .build();

        MonologFXButton.Type retval = mono.showDialog();


MonologFXButton mlb = MonologFXButtonBuilder.create()
        .defaultButton(true)
        .icon("/testmonologfx/dialog_apply.png")
        .type(MonologFXButton.Type.YES)
        .build();

 

MonologFXButton mlb2 = MonologFXButtonBuilder.create()
        .cancelButton(true)
        .icon("/testmonologfx/dialog_cancel.png")
        .type(MonologFXButton.Type.NO)
        .build();

 

MonologFX mono = MonologFXBuilder.create()
        .modal(true)
        .type(MonologFX.Type.QUESTION)
        .message("Welcome to MonologFX! Does this look like it might be useful?")
        .titleText("Important Announcement")
        .button(mlb)
        .button(mlb2)
        .buttonAlignment(MonologFX.ButtonAlignment.RIGHT)
        .build();

 

MonologFXButton.Type retval = mono.showDialog();

 


Extra Credit

Thanks to everyone who offered ideas for improvement and/or extension to the functionality contained within DialogFX. The JFXtras team welcomed it into the fold, and while I doubt there will be a need to include MonologFX in JFXtras, team members Gerrit Grunwald & Jose Pereda Llamas volunteered templates and i18n expertise to make MonologFX what it is. Thanks for the push, guys!

Where to Get (Git!) It

If you'd like to check it out, point your browser to the MonologFX repository on GitHub. Full source code is there, along with the current .jar file. Please give it a try and share your thoughts! I'd love to hear from you.

All the best,
Mark

Monday Sep 24, 2012

Join Me at JavaOne!

JavaOne 2012 is less than a week away! If you've already made plans to be there, you're probably getting pretty excited about it already...and if not, what are you waiting for?!?

Before I get to the session information, I want to point out that qualified students get free admission to JavaOne, so if you are (or know) a CS or IT (or other tech-leaning) student who might like to attend, follow the link and start making plans. There is so much there to learn and experience.

I'm happy to say I'll be a small part of the festivities. I'll be leading the following session:

CON3519 - Building Hybrid Cloud Apps: Local Databases + The Cloud = Extreme Versatility

In this session, learn how to design and develop applications that leverage both local storage and the cloud, maximizing the strengths of each. Using NetBeans, JavaServer Faces 2.0, GlassFish Server technology, JavaFX 2, Oracle Database, and Evernote, rapidly create prototypical applications that can be deployed in various environments and scaled up/out with enterprise cloud solutions. 

As a contributor to the JFXtras project, I also hope to attend the following "Birds Of a Feather" (BOF) session led by Gerrit Grunwald and Stephen Chin:

BOF5503 - JFXtras Super Happy Dev BOF

JFXtras, the open source JavaFX control and extensions project, is back for JavaFX 2.0. In this session, you will learn about the latest changes in JFXtras 2.0, including new components, controls, and features that integrate with the JavaFX 2.0 libraries. Expect to meet the JFXtras core team members as well as other interesting client RIA implementers and developers. Now that JavaFX is coded in Java, a few server-side hackers may even be let in the door.

If you're there, please stop by and introduce yourself! And to follow along with my J1 travels or keep in contact afterward, please follow me on Twitter or connect via G+ or Facebook (links in panel to right). Hope to see you there, but either way, keep the Java flowing!

All the best,
Mark 

About

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 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, three kids, and dog in the St. Louis, MO area.



Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today