Created
February 16, 2014 10:53
-
-
Save vigoo/9032435 to your computer and use it in GitHub Desktop.
Binding table's row count to a label with ScalaFX
This file contains hidden or 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
<?xml version="1.0" encoding="UTF-8"?> | |
<?import java.lang.*?> | |
<?import java.util.*?> | |
<?import javafx.scene.control.*?> | |
<?import javafx.scene.layout.*?> | |
<?import javafx.scene.paint.*?> | |
<?import javafx.scene.text.*?> | |
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="scalafxml.demo.rowcount.RowCountController"> | |
<children> | |
<TableView fx:id="table" prefHeight="349.0" prefWidth="572.0" AnchorPane.bottomAnchor="37.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0"> | |
<columns> | |
<TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="550.0" text="Test table" fx:id="column" /> | |
</columns> | |
</TableView> | |
<Label text="Number of rows:" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" /> | |
<Label fx:id="rowCount" alignment="CENTER_LEFT" prefWidth="410.0" text="#" textFill="#ff3333" AnchorPane.bottomAnchor="13.0" AnchorPane.leftAnchor="110.0"> | |
<font> | |
<Font name="System Bold" size="12.0" /> | |
</font> | |
</Label> | |
<Button mnemonicParsing="false" onAction="#addRow" text="Add row" AnchorPane.bottomAnchor="8.0" AnchorPane.rightAnchor="14.0" /> | |
</children> | |
</AnchorPane> |
This file contains hidden or 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
package scalafxml.demo.rowcount | |
import scalafx.Includes._ | |
import scalafxml.core.macros.sfxml | |
import scalafx.scene.control.{TableColumn, Label, TableView} | |
import scalafx.event.ActionEvent | |
import scalafx.application.JFXApp | |
import scalafxml.core.{DependenciesByType, FXMLView} | |
import scalafx.scene.Scene | |
import scalafx.beans.property.StringProperty | |
import javafx.beans.binding.StringBinding | |
// Sample data model for the table | |
class Data(text_ : String) { | |
val text = new StringProperty(this, "text", text_) | |
} | |
// Our controller class | |
@sfxml | |
class RowCountController( | |
private val rowCount: Label, // label to bind the row count to | |
private val table: TableView[Data], // the table | |
private val column: TableColumn[Data, String]) { // its only column, only for setting up its binding | |
// Showing Data.text in the table's only column | |
column.cellValueFactory = { _.value.text } | |
// Binding the table's item count to the label | |
rowCount.text <== new StringBinding { | |
bind(table.items.get()) // updated when the observable list is updated | |
def computeValue() = table.items.get().size().toString | |
} | |
// Button event handler to add new rows to the table | |
def addRow(event: ActionEvent) { | |
table.items.get().add(new Data("Test row")) | |
} | |
} | |
object RowCountDemo extends JFXApp { | |
val root = FXMLView(getClass.getResource("rowcount.fxml"), new DependenciesByType(Map())) | |
stage = new JFXApp.PrimaryStage() { | |
title = "Row count binding demo" | |
scene = new Scene(root) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OMG! I was about to throw my notebook through window, but that exemple helped me a lot(to code and to not throw my notebook through window)! +1000 thanks.