JavaFX: Custom controls template

Update:
This template is made for JavaFX 1.2! For 1.3 use the template posted here instead.

Here is a small template that helps you to create your own custom control without any problem:

[cc lang="c"]
public class MyControl extends Control {
override function create(): Node {
if ( skin == null ){
skin = MySkin{};
}
super.create();
}
}

public class MySkin extends Skin {
def myBehavior = bind behavior as MyOwnBehaviour;
def myControl = bind control as MyOwnControl;

init {
node = Rectangle {
width: bind control.width
height: bind control.height
}
behavior = MyOwnBehaviour{};
}

override function getPrefWidth( number ) {
//return your pref width – hard coded values are ok for most cases
}

override function getPrefHeight( number ) {
//return your pref height – hard coded values are ok for most cases
}

/*
//It is not absolutly necessary to override those methods, but strongly recommended.
override function getMinWidth() {
}

override function getMinHeight() {
}

override function getMaxWidth() {
}

override function getMaxHeight() {
}
*/

override function contains( localX: Number, localY: Number ): Boolean {
node.contains( localX, localY );
}

override function intersects( localX: Number, localY: Number, localWidth: Number, localHeight: Number ): Boolean {
node.intersects( localX, localY, localWidth, localHeight );
}
}

public class MyOwnBehaviour extends Behavior {
def myControl = bind skin.control as MyOwnControl;
}
[/cc]


4 Responses to “JavaFX: Custom controls template”

Leave a Reply