2

I am working on a javafx Minesweeper game, and currently am only using left mouse button input. I would like to use the right mouse button also so users can flag possible bombs. I looked at the Oracle webpage for Button class, and it says:

"When a button is pressed and released a ActionEvent is sent. Your application can perform some action based on this event by implementing an EventHandler to process the ActionEvent. Buttons can also respond to mouse events by implementing an EventHandler to process the MouseEvent."

https://docs.oracle.com/javafx/2/api/javafx/scene/control/Button.html

Ive tried this several different ways, with no success.

Included is my current EventHandler code. If anyone can explain the best way to handle right/left mouse clicks, or point me in the right direction of where to find that information, it is greatly appreciated.

MineButton is a custom class that extends Button. I would like on right click to mark as "m" and change cell color, and left click would remain the same.

 for (int row = 0; row < 8; row++){
 for (int col = 0; col <8; col++){
 MineButton button = new MineButton(row, col);
 button.setPrefSize(100, 100);
 button.setText("?");
 button.setStyle("-fx-font: 22 arial; -fx-base:#dcdcdc;");
 button.setOnAction(new EventHandler<ActionEvent>() {
 @Override
 public void handle(ActionEvent event) {
 if ( button.isAMine() == true){
 button.setText("B");
 for( int row1 = 0; row1 < 8; row1++){
 for ( int col1 = 0; col1 < 8; col1++){
 if (mineButtons[row1][col1].isAMine() == true){
 mineButtons[row1][col1].setText("B");
 mineButtons[row1][col1].setStyle("-fx- font: 22 arial; -fx-base: #dc143c;");
 }
 }
 }
 }
 else{
 recursion(mineButtons, button.getX(), button.getY());
 } 
 } 
 }); 
asked Dec 9, 2015 at 6:08

3 Answers 3

4

You cannot handle right clicks on a button by attaching an event handler on action event. Instead you need to add a handler to the mouse event:

button.setOnMouseClicked(new EventHandler<MouseEvent>() {
 @Override
 public void handle(MouseEvent mouseEvent) {
 if(mouseEvent.getButton().equals(MouseButton.SECONDARY)){
 System.out.println("Set flag on the button");
 }
 }
});
answered Dec 9, 2015 at 6:54
Sign up to request clarification or add additional context in comments.

Comments

2

If u wanna handle the MouseEvent use this code. It will work.

button.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
 @Override
 public void handle(MouseEvent event) {
 if(event.getButton() == MouseButton.SECONDARY){
// Type code to set flag here
 }
 }
 });
answered Dec 9, 2015 at 9:36

1 Comment

Added this to my code prior to my EventHandler, worked perfectly! Being able to mark mines made my minesweeper game that much closer to the original. Now I can add a timer, mines left counter, and a function to determine if player has one. I think the function to determine a win will check to see if all spaces have been marked or uncovered except for mines.
0
 scene.setOnMousePressed(event ->{
 if(event.getButton() == MouseButton.PRIMARY) {
 anchorX = event.getSceneX();
 anchorY = event.getSceneY();
 anchorAngleX = angleX.get();
 anchorAngleY = angleY.get();
 }
 });
 scene.setOnMouseDragged((MouseEvent event) -> {
 if(event.getButton() == MouseButton.PRIMARY) {
 angleX.set(anchorAngleX - (anchorY - event.getSceneY()));
 angleY.set(anchorAngleY - (anchorX - event.getSceneX()));
 }
 });

This code rotates a JavaFX group in a scene with a left mouse button and a drag. You can print the event.getButton to make certain your primary and secondary are working. I have tried many other ways including JavaFXtras. This is by far the simplest way to handle the primary and secondary mouse click events.

Pierre.Vriens
2,11775 gold badges32 silver badges44 bronze badges
answered Oct 18, 2019 at 4:20

1 Comment

This code rotates a JavaFX group in a scene with a left mouse button and a drag. You can print the event.getButton to make certain your primary and secondary are working. I have tried many other ways including JavaFXtras. This is by far the simplest way to handle the primary and secondary mouse click events.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.