我正在尝试使用TableView创建一个Jruby应用程序,但是我还没有能够用数据填充表,甚至找不到这样做的示例代码。下面是我的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>下面是相关的ruby代码:
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有没有人能建议我做错了什么,或者给我一个可以工作的示例代码?
发布于 2017-11-16 05:37:15
请参阅contrib/fxmltableview示例;我认为这正是您想要做的。您遇到的问题是,PropertyValueFactory是一个Java类,它试图访问一个属于JRuby类的Person。默认情况下,这不会像这个问题所显示的那样工作,但您可以通过调用Person.become_java!轻松解决这个问题。然而,即使您这样做了,它也不会工作,因为PropertyValueFactory需要[javatype] get[PropertyName]()形式的getter方法,而attr_accessor只生成[rubytype] [propertyname]()形式的getter方法。要解决此问题,请改用fxml_accessor,它会生成适当的方法(但不使用@变量,这些是原始属性实例):
class Person
include JRubyFX # gain access to fxml_accessor
# must specify type as the concrete `Property` implementation
fxml_accessor :name, SimpleStringProperty
fxml_accessor :address, SimpleStringProperty
def initialize
# note use of self to call the method Person#name= instead of creating local variable
self.name = 'foo'
self.address = 'bar'
# could also technically use @address.setValue('bar'), but that isn't as rubyish
end
end
# become_java! is needed whenever you pass JRuby objects to java classes
# that try to call methods on them, such as this case. If you used your own
# cellValueFactory, this probably would not be necessary on person, but still
# needed on your CVF
Person.become_java!https://stackoverflow.com/questions/44751645
复制相似问题