Home > javafx > JavaFX – Skinnable Controls

JavaFX – Skinnable Controls


JavaFX 1.3 introduces many new controls with CSS support. Please refer to UI Controls documentation, Style-Editor sample 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:

var dzone_url = “http://blogs.sun.com/rakeshmenonp/entry/javafx_skinnable_controls”;
var dzone_style = ‘2’;

digg_skin = ‘compact’;
digg_window = ‘new’;

Categories: javafx Tags: , ,
  1. Ashwani Rawat
    July 12, 2009 at 11:28 PM

    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. July 13, 2009 at 9:06 PM

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

  3. bikkie
    August 10, 2009 at 8:49 AM

    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]
    August 23, 2009 at 12:56 AM

    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. August 24, 2009 at 10:44 PM

    @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. September 9, 2009 at 5:11 AM

    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. September 9, 2009 at 5:32 AM

    @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. September 9, 2009 at 9:01 AM

    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. September 9, 2009 at 8:22 PM

    @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

  1. No trackbacks yet.

Leave a reply to Ashwani Rawat Cancel reply