Question

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.

Was it helpful?

Solution

Change IntegerProperty to ScalaFX ObjectProperty[Int], simply:

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

The rest can stay the same.

OTHER TIPS

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);
  }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top