Skip to content

Instantly share code, notes, and snippets.

@huyu398
Created June 13, 2014 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huyu398/4b4c4e29533af3a85592 to your computer and use it in GitHub Desktop.
Save huyu398/4b4c4e29533af3a85592 to your computer and use it in GitHub Desktop.
Customize ListView in JRubyFX with FXML
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Label?>
<HBox xmlns:fx="http://javafx.com/fxml" fx:id="hbox">
<children>
<Label fx:id="label1"/>
<Label fx:id="label2"/>
</children>
</HBox>
require 'java'
require 'jrubyfx'
fxml_root(File.dirname(__FILE__))
class ListViewApp < JRubyFX::Application
def start(stage)
with(stage, title: 'ListViewApp') do
fxml ListViewController
show
end
end
end
class ListViewController
include JRubyFX::Controller
fxml 'listview.fxml'
def initialize
array = %w(string1 string2 string3 string4 string5)
observable_list = FXCollections.observable_list(array)
@list_view.items = observable_list
@list_view.cell_factory = proc { ListViewCell.new }
end
end
class ListViewCell < Java.javafx.scene.control::ListCell
include JRubyFX::DSL
# rubocop:disable Style/MethodName
def updateItem(string, empty)
super(string, empty)
return if empty
data = Data.new
data.info(string)
set_graphic(data.hbox)
end
end
class Data
include JRubyFX
fxml_accessor :label1, SimpleStringProperty
fxml_accessor :label2, SimpleStringProperty
fxml_accessor :hbox, SimpleObjectProperty
def initialize
fxml_loader = JRubyFX::Controller.get_fxml_loader('list_cell_item.fxml')
fxml_loader.controller = self
fxml_loader.load
end
def info(string)
@label1 = string
@label2 = string
end
end
Data.become_java!
ListViewApp.launch
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.ListView?>
<GridPane xmlns:fx="http://javafx.com/fxml" alignment="CENTER">
<ListView fx:id="list_view"/>
</GridPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment