JavaFX: Custom controls template
- April 19th, 2010
- Posted in Java . JavaFX
- Write comment
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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; } |
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!