Eric Bruno

Dr. Dobb's Bloggers

A JavaFX File Editor: Part 2

November 27, 2012

In this entry, I'll talk about the JavaFX WebView component, the simple file editor itself, and keyboard processing.

In my previous blog entry, I discussed a simple JavaFX file editor as a way to introduce menu and tab processing. In this entry, I'll finish the story by talking about the JavaFX WebView component, the simple file editor itself, and keyboard processing.

The Content Interface

This simple application is all about opening tabs with some sort of relevant content within them. Therefore, I created a Content interface to manage this. No matter what I put in a tab, it must implement the Content interface so that the application can operate on it in some way.

public interface Content {
public Node getRoot();
}

It's a simple interface: The only method it needs is one that returns the content's root Node object so that it can be placed within a Tab, and cast to one of the known content type classes. Doing so allows the application to prompt the user to save when the application exits, for example.

One example where this interface is used is when the user creates a new tab, either through the File..New or the View..Web Page menu choice. Although the first opens a new editor and the second opens a browser (within a tab), there's a lot of shared processing:

Here, the proper content type is created (editor or browser). It's referenced as a Content interface, and the getRoot method is used to place the Node within the Tab.

The Web Browser

The WebBrowser class encapsulates everything we need to browse the Web from within a JavaFX application. This includes a TextField where the user can type a URL, a "Go" button the user can press to navigate to the URL, and the HTML5-compatible WebView control to display the actual web content.

All of the content is placed within a root VBox Node, including an HBox to contain the TextField and "Go" button side-by-side. A listener is created to track when the WebView's content changes; i.e., via a click on a link, so that the URL can be updated within the TextField. Another listener ensures that the URL is formatted correctly when the Go button is pressed to begin navigation.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!