JavaFX 1.3: Template for custom controls

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

Just paste this code into your fx file and rename the classes using the IDE you prefer…

[cc lang="c"]
public class MyOwnControl extends Control {
init {
if ( skin == null ) {
skin = MyOwnControlSkin {};
}
}
}

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

init {
behavior = MyOwnBehaviour{};

node = Rectangle {
width: bind control.width
height: bind control.height
}
}

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 getMaxWidth() {
//Return the max width
}

override function getMaxHeight() {
//Return the max height
}

override function getMinWidth() {
//Return the min width
}

override function getMinHeight() {
//Return the min height
}*/

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]


3 Responses to “JavaFX 1.3: Template for custom controls”

Leave a Reply