You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Patrick Plenefisch edited this page May 20, 2013
·
1 revision
What is this?
This document explains the extra syntactic sugar JRubyFX applies to JavaFX. All Java-style versions always work, this is just special ruby versions of the syntax.
Creating classes (aka the DSL)
All classes (including custom controllers) that don't conflict with a name support construction in the DSL style as a snake_cased version of their names. See the Getting Started Guide for more details.
Note: the blocks are executed in the context of the object and as such, local instance variables and methods of the creating class are not visible. you must save everything inside local variables
cached=get_other_data(@index)obj=my_custom_control(@some_data)do# self is what will become obj. we are inside the other objectself.other_data=cachedend
but is not the same as
obj=my_custom_control(@some_data)doself.other_data=get_other_data(@index)# here @index refers obj.instance_variable_get(:@index) and# get_other_data is MyCustomControl#get_other_dataend
Enums and constants (including Color)
All JavaFX enums and some constants can be specified as a ruby symbol in setters and constructors. This includes most usages of Color.
If you have a constant or enum that is not being converted to a Java enum properly (easy to spot because of Exception running Application: #<TypeError: cannot convert instance of class org.jruby.RubySymbol to _CLASS_TYPE_>), it is proabbly a bug and please report it. Note that you must use either the build family of functions (build, with, and DSL class_type()), or the snake_cased= version of the setter. setProperty is not overridden in case you want to avoid conversions.
Example
The following two lines are equivilent
stage.init_style=:transparent
stage.init_style=StageStyle::TRANSPARENT
Some Non-enum constants also work:
timeline.cycle_count=:indefinite
Duration
Any duration can be specified the java way (Duration.millis(500)) or with the monkey-patched Number class:
There are three types of animation syntaxes: Java style, multiple style, and single style. Java style is just java, use google for examples.
Single Style
Animate is a method on the timeline:
rectangle(x: 10,y: 40,width: 50,height: 50,fill: :red)do# note we must save this here as the property is# on the rectangle, not the timelinetranslate_x=translateXPropertytimeline(cycle_count: :indefinite,auto_reverse: true)do# animates given property for given timeline# marks, over given valuesanimatetranslate_x,0.sec=>1.sec,0=>200end.play# play immediatlyend
Some classes are probbably missing this, please report this.
Builder methods
Many classes have automatic adding of children, like Panels and Timelines. To utilize, create the object inside one of the builders (build, with, or the DSL class_name). See the analog_clock example for details.
Automatic adding
When you create an object inside another dsl-based method, it will automatically add the object as a child of the first. Ex:
sp=stack_panedolabel("hello!")end
which is the same as:
sp=stack_pane()sp.children.add(label("hello!"))
However, if you DON'T want this behavior, append a ! to the end of the type. ex:
When creating controllers, Controller.new calls initialize after the FXML has been loaded. Nodes with fx:ids (or plain ids) can be accessed by class instance variables (ex. @my_grid_pane).
Finding nodes
@my_fx_id# node or nilfind(css)# returns one node or nilfind!(css)# returns one node or raises an exceptioncss(css)# returns all matching nodes or an empty arrayscene[css]# returns all matching nodes or an empty array