2013-03-18 59 views
3

我是ScalaFX的新手。我正在嘗試修改一個基本的TableView示例,以包含Integer列。ScalaFX中的整數列TableView

到目前爲止,我想出了下面的代碼:

class Person(firstName_ : String, age_ : Int) { 
    val name = new StringProperty(this, "Name", firstName_) 
    val age = new IntegerProperty(this, "Age", age_) 
} 

object model{ 
    val dataSource = new ObservableBuffer[Person]() 
    dataSource += new Person("Moe", 45) 
    dataSource += new Person("Larry", 43) 
    dataSource += new Person("Curly", 41) 
    dataSource += new Person("Shemp", 39) 
    dataSource += new Person("Joe", 37) 
} 

object view{ 
    val nameCol = new TableColumn[Person, String]{ 
    text = "Name" 
    cellValueFactory = {_.value.name} 
    } 

    val ageCol = new TableColumn[Person, Int]{ 
    text = "Age" 
    cellValueFactory = {_.value.age} 
    } 
} 

object TestTableView extends JFXApp { 
    stage = new PrimaryStage { 
    title = "ScalaFx Test" 
    width = 800; height = 500 
    scene = new Scene {  
     content = new TableView[Person](model.dataSource){ 
     columns += view.nameCol 
     columns += view.ageCol 
     } 
    } 
    } 
} 

的問題是,雖然nameCol效果很好,ageCol甚至不進行編譯。

在行cellValueFactory = {_.value.age},我得到一個類型不匹配錯誤。它期待ObservableValue[Int,Int],但得到IntegerProperty

我正在使用爲Scala 2.10編譯的ScalaFX 1.0 M2。

回答

3

變化IntegerProperty到ScalaFX ObjectProperty[Int],簡單地說:

val age = ObjectProperty(this, "Age", age_) 

的其他部分可以保持不變。

+2

謝謝!你能否詳細說明爲什麼會發生這種情況? – Eduardo 2013-03-19 07:17:25

0

所以儘量...

TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name"); 

或錶行動

TableColumn<Person, Boolean> actionCol = new TableColumn<>("Action"); 
actionCol.setSortable(false); 
actionCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person, Boolean>, ObservableValue<Boolean>>() { 
    @Override public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Person, Boolean> features) { 
    return new SimpleBooleanProperty(features.getValue() != null); 
    } 
});