Search code examples
scalamouseeventscalafx

Trying to get user's latest mouse click in scalafx


I'm trying to get user's latest mouse click in order to display the right table. However, I can't find any way to implement this idea. How do i get user's latest mouse click by using mouseEvent function?

I tried using if else statements but it doesn't work when there is still value in the monstersTable1

def handleEditMonster(action : ActionEvent) = {
    val selectedMonster1 = monstersTable1.selectionModel().selectedItem.value
    val selectedMonster2 = monstersTable2.selectionModel().selectedItem.value

    if (selectedMonster1 != null){
        val okClicked = MainApp.showMonsterEditDialog(selectedMonster1)
        if (okClicked) showMonstersDetails(Some(selectedMonster1))

    } else if (selectedMonster2 != null) {
        val okClicked = MainApp.showMonsterEditDialog(selectedMonster2)
        if (okClicked) showMonstersDetails(Some(selectedMonster2))

    } else {
        // Nothing selected.
        val alert = new Alert(Alert.AlertType.Warning){
          initOwner(MainApp.stage)
          title       = "No Selection"
          headerText  = "No monsters Selected"
          contentText = "Please select a monsters in the table."
        }.showAndWait()
      }
    }

I want it to be able to access the second table even though selectedMonster1 is still != null


Solution

  • It's not entirely clear from your question what it is you're trying to do, so please bear with me... (For future reference, it's best if you can create a ''minimal, complete and verifiable example'' that illustrates your problem.)

    I'm assuming that you have two scalafx.scene.control.TableView instances, referenced via monstersTable1 and monstersTable2. You want to allow the user to select either one of the monsters in the first table, or one of the monsters in the second table, but not to be able to select one monster from each table simultaneously.

    I'm unclear when your handleEditMonster function is called, so I'm guessing that it's invoked when the user clicks, say, an Edit Monster button, as that button's clicked event handler.

    Do I have that right?

    Assuming the above is accurate, you should listen for changes in table selection, and clear the selection in the other table when a new selection is made. The currently selected item in each table is a property that we can add a listener to, so we can achieve this with the following code (in your scene's initialization):

    // In the onChange handlers, the first argument references the observable property
    // that has been changed (in this case, the property identifying the currently
    // selected item in the table), the second is the property's new value and the third
    // is its previous value. We can ignore the first and the third arguments in this
    // case. If the newValue is non-null (that is, if the user has made a
    // selection from this table), then clear the current selection in the other
    // table.
    monstersTable1.selectionModel.selectedItem.onChange {(_, newValue, _) =>
      if(newValue ne null) monstersTable2.selectionModel.clearSelection()
    }
    monstersTable2.selectionModel.selectedItem.onChange {(_, newValue, _) =>
      if(newValue ne null) monstersTable1.selectionModel.clearSelection()
    }
    

    This should do the trick for you, and your handleEditMonster function should now work. You might want to add an assertion to guard against both tables having a current selection, which would indicate a bug in the selection handler logic.