0

I am just trying to get familiar with ScalaFX, and my Scala knowledge is also still in embryo phase. Can you please help me out, in the following simple problem?

import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.collections.ObservableBuffer
import scalafx.scene.Scene
import scalafx.scene.control.TableColumn._
import scalafx.scene.control.{TableCell, TableColumn, TableView}

object Gui extends JFXApp {

case class listInfo(text: String, subtexts: List[String], thumbnail: String, attributeField: Int)

  val demo = ObservableBuffer[listInfo](
    new listInfo("dewdfasc", subtexts = List("first", "second"), "tdsam", 1),
    new listInfo("hgfhfghn", subtexts = List("first", "second"), "tdsacdsm", 2)
  )

  stage = new PrimaryStage {
    title = "ScalaFX Hello World"
    scene = new Scene {
      root = TableView[listInfo](demo) {
        columns ++= List(
          new TableColumn[listInfo, String] {
            text = "band"
            cellValueFactory = {_.value.text}
            prefWidth = 100
          },
          new TableColumn[listInfo, String] {
            text = "thumbnail"
            cellValueFactory = {_.value.thumbnail}
            prefWidth = 100
          }
        )
      }
    }
  }

}

has the following compiler error:

Error:(20, 23) object TableView does not take type parameters.
      root = TableView[listInfo](demo) {
Error:(21, 9) not found: value columns
        columns ++= List(
Error:(24, 41) type mismatch;
 found   : String
 required: scalafx.beans.value.ObservableValue[String,String]
            cellValueFactory = {_.value.text}
Error:(29, 41) type mismatch;
 found   : String
 required: scalafx.beans.value.ObservableValue[String,String]
            cellValueFactory = {_.value.thumbnail}

my environment:

java 1.8

scala 2.12

scalafx 2.11-8.0.144-R12

What should I correct, so that it compiles?

Thanks in advance!

bayerb
  • 649
  • 2
  • 9
  • 28

1 Answers1

0
import scalafx.application.JFXApp
import scalafx.beans.property.StringProperty
import scalafx.collections.ObservableBuffer
import scalafx.scene.Scene
import scalafx.scene.control.TableColumn._
import scalafx.scene.control.{ TableColumn, TableView }

object Gui extends JFXApp {
    case class ListInfo(_text: String, _subtexts: List[String], _thumbnail: String, _attributeField: Int) {
        val text = new StringProperty(this, "text", _text)
        val thumbnail = new StringProperty(this, "thumbnail", _thumbnail)
    }

    val demo = ObservableBuffer[ListInfo](
        ListInfo("dewdfasc", List("first", "second"), "tdsam", 1),
        ListInfo("hgfhfghn", List("first", "second"), "tdsacdsm", 2)
    )

    stage = new JFXApp.PrimaryStage {
        title.value = "ScalaFX Hello World"
        scene = new Scene {
            root = new TableView[ListInfo](demo) {
                columns ++= List(
                    new TableColumn[ListInfo, String] {
                        text = "band"
                         cellValueFactory = _.value.text
                         prefWidth = 100
                    },
                    new TableColumn[ListInfo, String] {
                        text = "thumbnail"
                        cellValueFactory = _.value.thumbnail
                        prefWidth = 100
                    }
                )
            }
        }
    }
}
  • Use uppercased types(e.g. ListInfo)
  • New keyboard is redundant when creating instanse of case class
  • cellValueFactory expecting scalafx.beans.value.ObservableValue[String,String] instead of string(see case class ListInfo val's)
Hayk Hakobyan
  • 309
  • 2
  • 8
  • After doing the adjustments you proposed, I still get 2 compiler error: Error:(26, 23) object TableView does not take type parameters. root = TableView[ListInfo](demo) { Error:(27, 9) not found: value columns columns ++= List( Is there an import missing? – bayerb Apr 15 '18 at 21:13
  • I was also using the same imports. – bayerb Apr 15 '18 at 21:32
  • Could you check your scalafx versioned at 8.0.144-R12? – Hayk Hakobyan Apr 15 '18 at 21:37
  • Yes, I can confirm, it is 8.0.144-R12. – bayerb Apr 15 '18 at 21:38
  • I have noticed you are using scala 2.12 but scalafx 2.11-8.0.144-R12 maybe the missmatch happens there? scalaVersion := "2.12.5" libraryDependencies += "org.scalafx" %% "scalafx" % "8.0.144-R12" this is my sbt configurations. – Hayk Hakobyan Apr 15 '18 at 21:42
  • Just tried with scala versions 2.11.8 and 2.12.4, and I get the same compile error in both cases. – bayerb Apr 15 '18 at 21:48
  • update scalafx 2.11-8.0.144-R12 to scalafx 2.12-8.0.144-R12 maybe that could help. – Hayk Hakobyan Apr 15 '18 at 21:50