Skip to content
Advertisement

Is there a way to check for a right mouse button click in FXML?

I am building a version of Risk in JavaFX with FXML for school. Now we want to be able to right click a button to decrease the amount of troops in a country and left click it to increase the amount. The left click was pretty self explanatory as it is just an onAction, but how would we check for a right click on a button, through FXML?

JavaScript

Advertisement

Answer

To react to a right mouse button click in javaFX, you would use the onMouseClicked event handler, and in the MouseEvent, check for which button was pressed using method getButton, which will return a MouseButton with value MIDDLE, PRIMARY, or SECONDARY

Controller code:

JavaScript

FXML:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement