Integer Columns in ScalaFX TableView
Asked Answered
O

2

5

I am new to ScalaFX. I am trying to adapt a basic TableView example, to include Integer columns.

So far, I have come up with the following code:

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
      }
    }
  }
}

The problem is that, while the nameCol works well, the ageCol doesn't even compile.

In the line cellValueFactory = {_.value.age}, I get a type mismatch error. It is expecting a ObservableValue[Int,Int] but getting an IntegerProperty.

I am using ScalaFX 1.0 M2, compiled for Scala 2.10.

Ockham answered 18/3, 2013 at 12:59 Comment(0)
S
7

Change IntegerProperty to ScalaFX ObjectProperty[Int], simply:

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

The rest can stay the same.

Serval answered 19/3, 2013 at 3:14 Comment(1)
Thank you! Could you please elaborate a little about why this happens?Ockham
M
0

so try...

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

or table action

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);
  }
});
Madder answered 18/3, 2013 at 16:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.