FX Experience Has Gone Read-Only

I've been maintaining FX Experience for a really long time now, and I love hearing from people who enjoy my weekly links roundup. One thing I've noticed recently is that maintaining two sites (FX Experience and JonathanGiles.net) takes more time than ideal, and splits the audience up. Therefore, FX Experience will become read-only for new blog posts, but weekly posts will continue to be published on JonathanGiles.net. If you follow @FXExperience on Twitter, I suggest you also follow @JonathanGiles. This is not the end - just a consolidation of my online presence to make my life a little easier!

tl;dr: Follow me on Twitter and check for the latest news on JonathanGiles.net.

Styling FX Buttons with CSS

Styling FX Buttons with CSS

A number of people have asked me recently can I create this look or that look using CSS in JavaFX. Or they have said that you could never do that! So I thought I would do a little experiment and try recreating a bunch of common button styles purely using CSS. So without further ado, here is the result:


(more…)

Correctly Checking KeyEvents

I was just writing a quick little application and I wanted to do something special whenever the user released the enter key inside a TextArea. You might be tempted to do it like this:

final TextArea editor = new TextArea();
editor.setOnKeyReleased(new EventHandler<KeyEvent>() {
    public void handle(KeyEvent t) {
        if (t.getCode() == KeyCode.ENTER) {
            System.out.println(editor.getText());
        }
    }
});

Can anybody spot the problem in this code? It correctly gets the key event, and it only prints out the text if the code of the key event is ENTER. The problem is that it also executes this code if you typed ALT+ENTER, or CTRL+ENTER, or any other combination of modifiers with the key code! JavaFX 2.0 contains a handy little class called KeyCombination which will handle all the annoying checking of key modifiers + key code to see if a specific key event matches. Here is the more correct version:

final TextArea editor = new TextArea();
editor.setOnKeyReleased(new EventHandler<KeyEvent>() {
    final KeyCombination combo = new KeyCodeCombination(KeyCode.ENTER);
    public void handle(KeyEvent t) {
        if (combo.match(t)) {
            System.out.println(editor.getText());
        }
    }
});

One extra line of code, and you get correct matching of the key events. Sweet!

FXML: Why It Rocks, And The Next Phase

JavaFX 2.0 shipped with a declarative XML-based language for defining user interfaces, called FXML. FXML is a key part of our strategy around making it easier to create user interfaces in Java. Certainly having a markup language has been attractive to web developers since it is a familiar and comfortable way to approach describing a user interface. But there are other key strategic reasons why FXML is important, how it fits into the broader JavaFX ecosystem, and how it helps you write testable user interfaces with minimal fuss. In essence, FXML helps you follow best practices while also making your life easier.

(more…)

SplitPane in JavaFX 2.0

SplitPane in JavaFX 2.0

We’ve introduced a SplitPane control in JavaFX 2.0, and today I thought I’d point out an interesting subtlety in the API. For the longest time our SplitPane API primarily consisted of the normal ‘left’ and ‘right’ (or ‘top’ and ‘bottom’) properties (indeed, the JavaDoc as of today still refers to this API). These were synonomous – if you set ‘top’ and ‘bottom’, they were literally copied to the ‘left’ and ‘right’ code, and our SplitPaneSkin just knew to draw with the items stacked vertically, rather than to lay them out horizontally.

(more…)

Indeterminate CheckBox

Indeterminate CheckBox

The CheckBox in JavaFX can be configured for two states (selected, or not) or three states (selected, unselected, or indeterminate). This indeterminate state is often useful when a checkbox is being used in a TreeView, for example. You might be implementing a tree view showing which features are installed, and need to toggle to an indeterminate state for the branch of some of the children are selected, and some are not.


(more…)