0

I'm trying to find some sample code to update my TableView from inside my controller. If possible I want to make my TableView with fxml.

def addPerson(event: ActionEvent) {
  // how do I access my TableView items?
}

My TableView looks like:

<TableView fx:id="tableView"></TableView>

Also, what's a good way to interactively inspect stage objects and methods?

pguardiario
  • 53,827
  • 19
  • 119
  • 159

1 Answers1

1

In order to use it, you need to pass it in as a parameter which will be shown below :

@sfxml
class PersonOverviewController(

    private val tableView : TableView[S] //S -> The type of the objects contained within the TableView items list.

    ) {

    def addPerson(event: ActionEvent) {
        // do whatever you want here with tableView
        val selectedIndex = tableView.selectionModel().selectedIndex.value //just for example 
    }
}
  • Do you set the fx:controller to it? It should be something like this `fx:controller="your.package.to.ControllerClass"` in your .fxml – Tony Ng Wei Shyang Jul 02 '17 at 07:08
  • The full length will be like : ` ` – Tony Ng Wei Shyang Jul 02 '17 at 07:13
  • Yes, thanks, I think I almost got it. what goes inside of [T] though? – pguardiario Jul 02 '17 at 07:33
  • It was parametric polymorphism. Says in the table you are trying to show the type Person, then it should be `TableView[Person]`. It is the same as in Java <> for example LinkedList type of Integer will goes like `LinkedList` . – Tony Ng Wei Shyang Jul 02 '17 at 07:37
  • I gave an upvote but I'm still getting errors: `value selectedIndex is not a member of javafx.scene.control.TableView.TableViewSelectionModel[sfxml.Person]` - maybe you can give a better example? – pguardiario Jul 02 '17 at 08:19
  • May I know if you import the javafx library or scalafx library? selectedIndex method is belongs to `scalafx.scene.control.TableView.TableViewSelectionModel` but not javafx's equivilent. Usually what we will do is we will import the scalafx's library, and import this : `import scalafx.Includes._` so that we can use both from scalafx and javafx – Tony Ng Wei Shyang Jul 02 '17 at 10:36
  • Thanks, that's working for me now with that import line. – pguardiario Jul 02 '17 at 23:05