søndag 30. september 2012

JavaFX 2.2 - The Swing Replacement

JavaFX started its life as DOA. Dead On Arrival. Rumors of being slow, its own script language, RIA competitor, etc. There was no reason to waste time having a look at it. There was no reason to use it since RIA software began a decline.

The new v2.0 was released October last year with nobody noticing. When the new version, v.2.2 released this summer, I sat down and had a look at it. Having previously worked on a  Java Swing project, I had to have a look, and I was surprised of all the new features..

The everyday story
Being a Java consultant also means you have to help out when tenders arrives from customers. The customer wants a brand new software and have delivered yet another of those terrible specifications that lack important details, and are filled with useless crap.

For this story we can assume the customer wants an desktop application.

The answer to this request is usually to make an web application no matter what, or even a .Net application. But there are good reasons for making desktop applications using Java. Java has a good track record of making old applications not brake when OS or hardware changes. That is very important.

If you are given the task of making a Java desktop application, then Swing will probably pop into your head. Right?

Forget Swing, and replace that with JavaFX 2.2. Swing is in fact dead. It will not be modernized in any way or made more useful. You can use JavaFX in the browser, and even drag the software onto the desktop. It can be used to create desktop application, mobile applications with touch capability, even embeddable in Swing.

I have had the privilege to work on one of my company projects where they are creating a Java desktop application with Swing. The funny thing about that project is that the customer have demanded the software to implement the Microsoft User Experience Interaction Guidelines. The customer will start every argument with starting Microsoft XL/Word/Outlook/Powerpoint and point on some stuff and say "it has to behave like this".

The answer is that Swing cannot mimic the interaction. It is not made for that. The project are in fact doing massive amount of work to rewrite lots of swing code in order to make it behave like XL.
The problem is that Swing was made with antiquated UX principles coded into its massive core.
Swing is also terrible to work with in order to make clean MVC code. The code will be massive, with lots of event listeners, threads and boilerplate stuff.

Lets talk about JavaFX
The central part in JavaFX is a Scene Graph. Every element inherits from a Node object, and that means you can put everything together the way you like. You can add CSS class names, and styles to Nodes. It is kind of comparable to Web and the DOM structure. If you have worked with web, then this is familiar to you.
In v2.0 Oracle removed the script language that put most of us off. In stead JavaFX has got a nice Java api that is familiar to you if you have worked with Swing. There is buttons, labels, textfield, and you put them in one of the many layout panes like the familiar BorderPane.

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

                public void handle(ActionEvent arg0) {
                        System.out.println("Hello World!");    
                }
        });
        
        BorderPane root = new BorderPane();
        root.setCenter(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

This is the example from the JavaFX Documentation site. As you can see, it is easy.

Want to add a style scheet?

scene.getStylesheets().add(Main.class.getResource("mystyles.css").toExternalForm());

Want to add a CSS class to an element?

btn.getStyleClass().add("button1");

Then the CSS..

.button1{
    -fx-text-fill: #006464;
    -fx-background-color: #DFB951;
    -fx-border-radius: 20;
    -fx-background-radius: 20;
    -fx-padding: 5;
}

I could write about the binding api which reduces the clutter of listeners, observables and boilerplate code to make nice MVC. Very cool indeed. The concurrency part is massively improved with Task and Services. There is a lot of exciting stuff that won't fit into a simple blog post.

But there is one downside. The look-and-feel is gone. This is putting people and customers off. I think there would be a lot of work in making a complete CSS that would mimic Windows. People don't like unfamiliar stuff.
But think about it. Spotify, Winamp, Photoshop, 3D Studio, modern browsers and chat software have for a long time rebelled against the traditional OS look and feel. There is no reason to have a look and feel. Good usability just require common sense. My company have several very skilled usability experts which can help in creating good interaction design if needed. There. Problem solved.

Can JavaFX mimic Microsoft XL? I thnik it will be easier with JavaFX but the idea is insane no matter what.

References:
JavaFX documentation site. Hello World, examples etc
http://docs.oracle.com/javafx/index.html

The Ensamble is a huge collection of small demos of features. Run it.
http://www.oracle.com/technetwork/java/javafx/samples/index.html

Article about the JavaFX architecture
http://docs.oracle.com/javafx/2/architecture/jfxpub-architecture.htm

http://docs.oracle.com/javafx/2/release_notes_2-2/jfxpub-release_notes_2-2.htm
http://www.oracle.com/technetwork/java/javafx/documentation/index.html

v1.0 - 30.sept.2012

2 kommentarer:

  1. Nice blog post. Hoping for a follow up where you show some java fx wow beyond hello world. And how about a screen shot?

    SvarSlett
  2. Thx. A few more posts, and github code has been posted already. I have a few more ideas I want to explore. But as always, time is scarce.

    SvarSlett