I'm trying to make a Jruby app with a TableView but I haven't been able to populate the table with data or even find some sample code to do so. Here's the relevant part of my fxml:
<TableView prefHeight="400.0" prefWidth="200.0" id="table">
<columns>
<TableColumn prefWidth="75.0" text="name">
<cellValueFactory>
<PropertyValueFactory property="name" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="75.0" text="address">
<cellValueFactory>
<PropertyValueFactory property="address" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
And here's the relevant ruby code:
class Person
attr_accessor :name, :address
def initialize
@name = 'foo'
@address = 'bar'
end
end
class HelloWorldApp < JRubyFX::Application
def start(stage)
with(stage, title: "Hello World!", width: 800, height: 600) do
fxml HelloWorldController
@data = observable_array_list
@data.add Person.new
stage['#table'].set_items @data
show
end
end
end
Can someone suggest what I'm doing wrong or point me to a working sample code?
See the contrib/fxmltableview sample; I think that is exactly what you want to do. The issue you are running into is the fact that
PropertyValueFactory
is a Java class, and it is trying to access aPerson
which is a JRuby class. By default this won't work as this question shows, but you can easily fix that by callingPerson.become_java!
. However, even if you do that, it won't work as thePropertyValueFactory
expects getter methods of the form[javatype] get[PropertyName]()
whereasattr_accessor
only generates getter methods of the form[rubytype] [propertyname]()
. To solve this, usefxml_accessor
instead, which generates the proper methods (but doesn't use@
vars, those are the raw property instances):