I have the gridpane set up just fine. But how to put event handler in each cell? Like mouse double click and right click. Could someone give me an example, please? Thank you!
Asked
Active
Viewed 7,168 times
1 Answers
0
You don't specify what you want exactly,but i hope this examples can help you
First example (I add the same event for all nodes)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package stackover;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
/**
*
* @author Ala_Eddine
*/
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private GridPane gridPane;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
addGridEvent();
}
private void addGridEvent() {
gridPane.getChildren().forEach(item -> {
item.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (event.getClickCount() == 2) {
System.out.println("doubleClick");
}
if (event.isPrimaryButtonDown()) {
System.out.println("PrimaryKey event");
}
}
});
});
}
**}
Second example**
private void addGridEvent() {
Button button = new Button("hi");
//You can use OnAction
button.addEventHandler(EventType.ROOT, (event) -> {
if (event.getEventType() == ActionEvent.ACTION) {
System.out.println("ActionEvent");
}
});
gridPane.getChildren().add(button);

Menai Ala Eddine - Aladdin
- 2,364
- 4
- 22
- 46