java - How to get GridPane Row and Column IDs on Mouse Entered in each cell of Grid in JavaFX? -
i trying out jfx drag , drop feature in application (later connecting objects in sequence). not find easy way drag , drop imageview/button @ suitable position in pane, hence planning apply use of gridpane. gridpane large canvas more (50x50) cells.
if specify in code need drag , drop imageview at, let's say, (2,2) cell, able it. but, need give access user. user move pointer in grid , drop imageview/button @ cell in grid.
now, find out rowid , columnid of cell on mouse entered in particular cell of gridpane.
i handling mouse event in controller 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"); } }
when enters in gridpane, "mouseentered" once. besides, not able rowid , columnid, hence, have not written function in function.
my interface looks following:
i know 2 things:
[] how detect mouse entry in each cell of gridpane?
[] how find out row , column ids of particular cell?
thanks in advance.
you can values calling gridpane.getcolumnindex(node)
, gridpane.getrowindex(node)
.
you haven't shown you're putting in grid pane, , whether populating in fxml or in java, here's basic functionality. here (tediously) populate gridpane
in fxml, might better populate in java (i.e. in controller), depending on use case. (that way, handler gets bit cleaner too.)
controller class:
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()); } }
if don't want define contents of cells in fxml, can define them in controller. makes things cleaner, can register listener when 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 ; (int = 0 ; < numcols ; i++) { columnconstraints colconstraints = new columnconstraints(); colconstraints.sethgrow(priority.sometimes); grid.getcolumnconstraints().add(colconstraints); } (int = 0 ; < numrows ; i++) { rowconstraints rowconstraints = new rowconstraints(); rowconstraints.setvgrow(priority.sometimes); grid.getrowconstraints().add(rowconstraints); } (int = 0 ; < numcols ; i++) { (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>
application:
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); } }
Comments
Post a Comment