如何在JavaFX中的Grid的每个单元格中输入鼠标上的GridPane行和列ID? [英] How to get GridPane Row and Column IDs on Mouse Entered in each cell of Grid in JavaFX?

查看:121
本文介绍了如何在JavaFX中的Grid的每个单元格中输入鼠标上的GridPane行和列ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中尝试JFX拖放功能(稍后按顺序连接对象)。我找不到任何简单的方法将我的ImageView / Button拖放到窗格中的适当位置,因此我计划应用GridPane。 GridPane将是一个包含超过(50x50)个单元格的大画布。

I am trying out JFX Drag and Drop feature in my application (later connecting the objects in a sequence). I could not find any easy way to drag and drop my ImageView/Button at suitable position in my pane, hence I am planning to apply use of GridPane. The GridPane would be a large canvas with more than (50x50) cells.

如果我在代码中指定我需要通过ImageView拖放,让我们说, (2,2)细胞,我能够做到。但是,我需要授予我的用户访问权限。用户可以在网格中移动指针,并将ImageView / Button放在网格中的任何单元格中。

If I specify in my code that I need to drag and drop by ImageView at, let's say, (2,2) cell, I am able to do it. But, I need to give that access to my user. User could move the pointer in grid and would drop the ImageView/Button at any cell in the grid.

现在,我想找出在GridPane的特定单元格中输入的鼠标单元格的rowID和columnID。

Now, I would like to find out rowID and columnID of the cell on mouse entered in a particular cell of the GridPane.

我在Controller中处理鼠标事件如下:

I am handling mouse event in my Controller as follows:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.*;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable{
Stage stage;

@FXML
private GridPane gridPane;

public void setStage(Stage primaryStage){
    stage = primaryStage;
}

@Override
public void initialize(URL location, ResourceBundle resources) {
    System.out.println("initialize");
}


@FXML
private void handleMouseEntered(MouseEvent event){
    System.out.println("MouseEntered");
}

}

当进入gridpane时,我只获得一次MouseEntered。此外,我无法获得任何RowID和ColumnID,因此,我没有在函数中编写任何函数。

When enters in the gridpane, I get "MouseEntered" only once. Besides, I am not able to get any RowID and ColumnID, hence, I have not written any function in the function.

我的界面如下所示:

My interface looks like following:

我想知道两件事:

[]如何在网格窗格的每个单元格中检测鼠标输入?

[] How to detect mouse entry in each cell of the gridpane?

[]如何找出特定单元格的行和列ID?

[] How to find out row and column IDs of the particular cell?

提前致谢。

推荐答案

您可以通过调用 GridPane.getColumnIndex(node) GridPane.getRowIndex(node)

你还没有展示你在网格窗格中放置的内容,甚至是用FXML还是用Java填充它,但这里有一些基本的功能。在这里,我(繁琐地)在FXML中填充 GridPane ,根据您的使用情况,最好用Java(即在控制器中)填充它。 (这样,处理程序也会变得更清晰。)

You haven't shown what you're putting in the grid pane, and even whether you are populating it in FXML or in Java, but here's some basic functionality. Here I (tediously) populate the GridPane in FXML, it might be better to populate it in Java (i.e. in the controller), depending on your use case. (That way, the handler gets a bit cleaner too.)

控制器类:

import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;


public class GridPaneTrackingController {

    @FXML
    private void mouseEntered(MouseEvent e) {
        Node source = (Node)e.getSource() ;
        Integer colIndex = GridPane.getColumnIndex(source);
        Integer rowIndex = GridPane.getRowIndex(source);
        System.out.printf("Mouse entered cell [%d, %d]%n", colIndex.intValue(), rowIndex.intValue());
    }
}

如果您不想定义内容在FXML中的单元格,然后您可以在控制器中定义它们。这使事情变得更清晰,因为你可以在定义它时注册监听器:

If you don't want to define the contents of the cells in FXML, then you can just define them in the controller. That makes things cleaner, as you can just register the listener when you define them:

import javafx.fxml.FXML;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;


public class GridPaneTrackingController {

    @FXML
    private GridPane grid ;

    public void initialize() {
        int numCols = 4 ;
        int numRows = 4 ;

        for (int i = 0 ; i < numCols ; i++) {
            ColumnConstraints colConstraints = new ColumnConstraints();
            colConstraints.setHgrow(Priority.SOMETIMES);
            grid.getColumnConstraints().add(colConstraints);
        }

        for (int i = 0 ; i < numRows ; i++) {
            RowConstraints rowConstraints = new RowConstraints();
            rowConstraints.setVgrow(Priority.SOMETIMES);
            grid.getRowConstraints().add(rowConstraints);
        }

        for (int i = 0 ; i < numCols ; i++) {
            for (int j = 0; j < numRows; j++) {
                addPane(i, j);
            }
        }
    }

    private void addPane(int colIndex, int rowIndex) {
        Pane pane = new Pane();
        pane.setOnMouseEntered(e -> {
            System.out.printf("Mouse enetered cell [%d, %d]%n", colIndex, rowIndex);
        });
        grid.add(pane, colIndex, rowIndex);
    }

}

FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.GridPane?>

<GridPane xmlns:fx="http://javafx.com/fxml/1" 
    fx:controller="GridPaneTrackingController" 
    fx:id="grid" gridLinesVisible="true">

</GridPane>

申请:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class GridPaneTrackingExample extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane root = FXMLLoader.load(getClass().getResource("GridPaneTrackingExample.fxml"));
        primaryStage.setScene(new Scene(root, 800, 800));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于如何在JavaFX中的Grid的每个单元格中输入鼠标上的GridPane行和列ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆