JavaFX – Skinnable Controls


JavaFX 1.3 introduces many new controls with CSS support. Please refer to UI Controls documentation, Style-Editor sample, Power of CSS and Dean Iverson’s Blog for quick start. Also refer to article Applying CSS to UI Controls.

Its long time since I blogged and lot of things happened during that time – Release of JavaFX 1.2, Java Store, New Samples… and lot more to write about! 🙂 Let me start with UI Controls

Following are some of the UI controls –
Button, CheckBox, Hyperlink, Label, ListView, ProgressBar, ProgressIndicator, RadioButton, ScrollBar, Slider, TextBox, ToggleButton

For Applet mode, click on above image

For standalone mode

The article on Node-Based Controls demonstrates its usage. So let me focus on how to update the skin of these controls.

The skin attribute of Control class allows you to associate a Skin. The Skin is responsible for implementing the user interface using on scene-graph nodes and defining the behavior of the control. The Behavior has the logic for handling key and mouse events.

Sounds complicated? confused? Control, Skin, Behavior.. For now lets look at a simple and straight forward way to just change the color and some basic attributes of Skin.

JavaFX already has a default skin implementation – Caspian. Its in com.sun.javafx.scene.control.caspian package. For each control, there is corresponding skin implementation. We can change few attributes of these skin implementations and customize the look of controls.

Example: To update skin of Button, we can customize the attributes of ButtonSkin implementation.


var button = Button {
    text: "Button"

    skin: ButtonSkin {
        borderFill: Color.BLACK
        focusFill: Color.BLUE
        highlightFill: Color.GREEN
        shadowFill: Color.GRAY
        textFill: Color.RED
        fill: Color.WHITE
    }

}

Similarly a TextBox can be customized as shown below:


var textBox = TextBox {
    text: "TextBox"

    skin: TextBoxSkin {
        backgroundFill: Color.YELLOW
        borderFill: Color.BLACK
        caretFill: Color.BLUE
        focusFill: Color.BEIGE
        highlightFill: Color.BLUE
        promptTextFill: Color.GRAY
        selectedTextFill: Color.WHITE
        shadowFill: Color.DARKGRAY
        textFill: Color.BLACK
    }

}

Please note, all those attributes are instance of Paint, so we can assign instance of Color, LinearGradient or RadialGradient.
More attributes and implementation is available in the source.
Try it out and let me know feedback!

Source:

Advertisements

About Rakesh Menon
-

11 Responses to JavaFX – Skinnable Controls

  1. Ashwani Rawat says:

    Hello Rakesh Menon,
    I is very good tuotorial for development in javafx. I have created autocomplete text box by using ListView and TextBox. But I am not able to restrict user to use Ctrl V (Past), Ctrl X(Cut), Ctrl Z(Undo) etc key. Is is any way in javafx to implement these restriction on text box.
    Thanks in advance
    Regards
    Ashwani Rawat

  2. Rakesh Menon says:

    @Ashwani Rawat You can extend TextBox and override methods cut, copy, paste to do no operation.

  3. bikkie says:

    Hi Ashwani,
    I also want to implement Auto complete in my POC.
    could you please share your code with me.
    thanks and regards,
    bikkie

  4. Nihar [ntimesc@gamil.com] says:

    Hi Rakesh ,
    your examples are really informative to me …
    but i want to know that where can i find the documentation of following classes
    com.sun.javafx.scene.control.caspian.ButtonSkin;
    com.sun.javafx.scene.control.caspian.ToggleButtonSkin

  5. Rakesh Menon says:

    @Nihar There are no documentation available as of now. The team is working on this. I have used all attributes that are accessible for ButtonSkin and ToggleButtonSkin in Main.fx Let me know if you are looking for anything beyond that, I can check and get back to you.

  6. nihar says:

    Hi Rakesh,
    I have found many interesting example on your blog which really accelerate me to develop rich internet application in JavaFX.
    But i have one doubt i.e. in JavaFX one can set background gradient using LinerGradient but if i have a image gradient in png format and i want to render it as a background on Rectangle with a repeat mode (like people do by setting repeat-x property in CSS) then what would be the approach to do that ?

  7. Rakesh Menon says:

    @nihar Thanks! I think you are referring to something like TexturePaint in JavaSE.. http://java.sun.com/javase/6/docs/api/java/awt/TexturePaint.html Its *not* available in JavaFX 1.2, its work in progress.

  8. nihar says:

    Thanks Rakesh ,
    One more thing as in JavaFX there is no Table/Grid Control exist as of now…
    So do u know what could be the other alternatives that looks good and easy to use/plug in javafx ?

  9. Rakesh Menon says:

    @nihar You may refer to http://jfxtras.org/ and http://code.google.com/p/jfxtras/ Another way is to use JavaSE Swing Table and embed in JavaFX using javafx.ext.swing.SwingComponent.wrap API

  10. Daniel says:

    Good article. But how do you skin a custom control?

  11. Rakesh Menon says:

    @Daniel This article is a bit old. This is more of workaround for JavaFX 1.2. I’ll write a better approach with CSS support once JavaFX 1.3 is released.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: