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]
April 19th, 2010 at 19:44
[...] If you are just looking for a template, continue here. [...]
April 20th, 2010 at 17:25
Very nice, Johannes, and very helpful. Creating custom controls in JavaFX has been quite the battle so far. Fortunately, a little bird told me that custom controls in JavaFX 1.3 will be improved and much easier.
Thanks for the post!
April 20th, 2010 at 17:28
[...] have updated the template [...]
April 23rd, 2010 at 13:45
[...] Custom controls has been really hard in 1.2. I have created a template that did what it should. But with the 1.3 release this template does no longer [...]