Scala Swing event framework - where do I add my reactors?

為{幸葍}努か 提交于 2019-12-18 17:04:34

问题


I'm trying to catch a mouse-click even on a Table (which should cause a popup to be shown). The table is inside a ScrollPane which is (in turn) inside a Panel. I have added reactions to all the classes, but I can never seem to actually get a click event to be caught!

class MyPanel extends GridBagPanel {
  val gbc = new GridBagContraints( ... )

  add(new ScrollPane {
    reactions += {
      case MouseClicked(src, point, mod, clicks, pops) =>
        println("Scroll pops: " + pops)
    } 

    viewportView = new Table {
      reactions += {
        case MouseClicked(src, point, mod, clicks, pops) =>
          println("Table pops: " + pops)
      } 

      ...
    }

  }, gbc)

  reactions += {
    case MouseClicked(src, point, mod, clicks, pops) =>
      println("Panel pops: " + pops)
  } 
}

No matter where I click, nothing gets printed. What am I doing wrong?


回答1:


OK - You have to listen to the correct thing:

class MyPanel extends GridBagPanel {
  val gbc = new GridBagContraints( ... )

  val table = new Table { ... }

  add(new ScrollPane {

    viewportView = table
  }

  }, gbc)

  listenTo(table.Mouse.clicks) //THIS LINE IS IMPORTANT :-)

  reactions += {
    case MouseClicked(`table`, point, mod, clicks, pops) =>
      println("Panel pops: " + pops)
    } 
  }
}


来源:https://stackoverflow.com/questions/938437/scala-swing-event-framework-where-do-i-add-my-reactors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!