Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. package taht;
  2.  
  3. import javafx.application.Application;
  4. import javafx.event.EventHandler;
  5. import javafx.scene.Node;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.Label;
  8. import javafx.scene.input.MouseEvent;
  9. import javafx.scene.layout.GridPane;
  10. import javafx.scene.layout.Pane;
  11. import javafx.scene.paint.Color;
  12. import javafx.stage.Stage;
  13.  
  14. public class Demo extends Application {
  15.  
  16. public static void main(String[] args) {
  17. Application.launch(args);
  18. }
  19.  
  20. @Override
  21. public void start(final Stage primaryStage) {
  22.  
  23. Pane root = new Pane();
  24.  
  25. GridPane gridPane = new GridPane();
  26. gridPane.setGridLinesVisible(true);
  27.  
  28. for (int i = 0; i < 5; i++) {
  29. for (int j = 0; j < 5; j++) {
  30.  
  31. Label label = new Label("Label " + i + "/" + j);
  32. label.setMouseTransparent(true);
  33. GridPane.setRowIndex(label, i);
  34. GridPane.setColumnIndex(label, j);
  35.  
  36. gridPane.getChildren().add(label);
  37. }
  38. }
  39.  
  40. root.getChildren().add(gridPane);
  41.  
  42. Scene scene = new Scene(root, 400, 300, Color.WHITE);
  43. primaryStage.setScene(scene);
  44.  
  45. primaryStage.show();
  46.  
  47. gridPane.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
  48. @Override
  49. public void handle(MouseEvent e) {
  50.  
  51. for (Node node : gridPane.getChildren()) {
  52.  
  53. if (node instanceof Label) {
  54. if (node.getBoundsInParent().contains(e.getSceneX(), e.getSceneY())) {
  55. System.out.println("Node: " + node + " at " + GridPane.getRowIndex(node) + "/" + GridPane.getColumnIndex(node));
  56. }
  57. }
  58. }
  59. }
  60. });
  61.  
  62. }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement