Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right Click on a Button / Scala

I'm currently trying to code a minesweeper using scala, but I can't find the way to listen to a right click on a button.

I've already searched on the Internet the way to do it, but I definitely was not able to find it.

If anyone could help me out, I would be really grateful :)

Thanks,

Schnipp

(Note: Scala is a new language to me and I am not a Java user, so I am sorry if my questions sound dumb)

EDIT:

I am trying to find (or implement) a function 'ButtonClickedRight' that could listen to a right-click on a button.

like this

import scala.swing._
import scala._
import scala.swing.event._

object Right extends MainFrame with App {
    title = ""
    visible = true

    val b = new button("")
    listenTo(b)
    reactions += {
       case ButtonClicked(`b`) => *code*
       case ButtonClickedRight(`b`) => *code*
    }
}

EDIT 2 --

I would like to know if the user has clicked on the Button "1" or not. The problem I have is that this code prints "Mouse clicked at " + e.point+" type "+e.modifiers when I click on the label but not on the button.

object App extends SimpleSwingApplication {
  lazy val ui = new GridPanel(2,1) {
    contents += new Button("1")
    contents += new Label("2")
    listenTo(mouse.clicks)
    reactions += {
      case e: MouseClicked =>
        println("Mouse clicked at " + e.point+" type "+e.modifiers)
    }
  }
  def top = new MainFrame {
    contents = ui
    visible = true
    preferredSize = new Dimension(500,500)
  }
}
like image 589
Schnipp Avatar asked Feb 22 '15 14:02

Schnipp


1 Answers

Button events are fired through a specific publisher .mouse.clicks.

import scala.swing._
import scala.swing.event._

object App extends SimpleSwingApplication {
  lazy val ui = new GridPanel(2,1) {
    val button = new Button("1")
    contents += button
    contents += new Label("2")
    listenTo(button.mouse.clicks) // !
    reactions += {
      case evt @ MouseClicked(`button`, pt, _, _, _) =>
        val which = evt.peer.getButton
        if (which > 1) {
          println(s"Mouse clicked at (${pt.x}; ${pt.y}) - button: $which")
        }
    }
  }
  lazy val top = new MainFrame {
    contents = ui
    size = new Dimension(500,500)
  }
}

Note that at least on Linux my right button has number 3 not 2. You could also use the triggersPopup flag, but then you must ensure to monitor both MousePressed and MouseReleased, as this flag is platform-dependent.

like image 120
0__ Avatar answered Oct 16 '22 00:10

0__