首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用Jruby/Jrubyfx填充TableView

无法使用Jruby/Jrubyfx填充TableView
EN

Stack Overflow用户
提问于 2017-06-26 07:58:56
回答 1查看 45关注 0票数 1

我正在尝试使用TableView创建一个Jruby应用程序,但是我还没有能够用数据填充表,甚至找不到这样做的示例代码。下面是我的fxml的相关部分:

代码语言:javascript
复制
<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代码:

代码语言:javascript
复制
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

有没有人能建议我做错了什么,或者给我一个可以工作的示例代码?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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,它会生成适当的方法(但不使用@变量,这些是原始属性实例):

代码语言:javascript
复制
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!
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44751645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档