Created
June 13, 2014 07:11
-
-
Save huyu398/4b4c4e29533af3a85592 to your computer and use it in GitHub Desktop.
Customize ListView in JRubyFX with FXML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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