Eric Bruno

Dr. Dobb's Bloggers

A JavaFX Text Editor: Part 1

November 18, 2012

I've built a scaled-down editor app that illustrates a lot of what makes JavaFX awesome for building client GUI applications.

Recently, I've been building a specialized file editor with JavaFX 2.x for my day job. While I can't share all the details of that project here, I can share the key findings I've made along the way. I've built a scaled-down editor app that illustrates a lot of what makes JavaFX awesome for building client GUI applications. In fact, despite all my work with JavaFX over the past 2+ years, there are many features I've just never worked with closely. Let's take a look at three of these features now: the TabPane, MenuBar, and FileChooser interfaces. In the second part, we'll explore keyboard processing, the WebView component, and the file editor itself.

Multi-Tab Editor

I've never created a multi-tabbed interface with JavaFX. It's not that I avoided it for any particular reason, it's just that I never had a reason to until recently. With JavaFX, it took me all of five minutes to learn what I needed. Here's a summary with some code.

First, create a TabPane component to contain the actual tabs:

TabPane tabPane = new TabPane();

Next, create a Tab to and place it into the TabPane:

Tab tab = new Tab();
tabPane.getTabs().add(tab);

Next, add content to the Tab itself (this can be a control or a layout component such as an HBox, VBox, Group, and so on):

tab.setContent(content.getRoot());

Set the tab's title text:

tab.setText("My New Tab");

By default, the tab is added to the end of existing tabs and is not selected. If you'd like the tab to become the selected, visible tab, call the following code:

The end result is a simple set of tabs in your application UI, as shown here.

￼

Next, let's examine another common navigation tool: menus.

JavaFX Menus and Processing

Believe it or not, I've never needed to create application menus with JavaFX until recently. All of my work with JavaFX has been building custom JavaFX controls to be integrated into an existing Java Swing application; hence no need for menus. Again, it took only a matter of minutes to learn the API and build the menu system my application needed. Here's how.

After adding the menus and menu items, you have something similar to this:

￼

Simple File Editor Beginnings

Below is the complete menu and tab code in the beginnings of the simple JavaFX file editor application. In the next part, we'll fill in the remaining details of the application, and I'll present all of the code.

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!