javafx - Assigning a MouseListener to each Region in a 2d array of Regions -
i posted question here couple weeks ago on making chess game , got great response , code demonstrates solution problem. have been trying dissect code , study can better understand how works. while doing ran question can not seem find answer for. chess board made of 2d array of regions , trying add mouselistener each region in 2d array when region pressed print out row , column of region pressed. right nothing happening when press on square in screen , can't not figure out why mouselistener not working.
public class main extends application { gridpane root = new gridpane(); final int size = 8; int col = 0; int row = 0; @override public void start(stage primarystage) { try { gridpane board = new gridpane(); region[][] fields = new region[8][8]; (int = 0; < fields.length; i++) { region[] flds = fields[i]; (int j = 0; j < flds.length; j++) { region field = new region(); flds[j] = field; field.setbackground(new background(new backgroundfill((i + j) % 2 == 0 ? color.white : color.lightblue, cornerradii.empty, insets.empty))); } board.addrow(i, flds); } (int = 0; < fields.length; i++) { col = i; (int j = 0; j < fields[i].length; j++) { row = j; fields[i][j].setonmouseclicked(e->{ system.out.println("col:" + col + ", row" + row); }); } } // use 1/8 of size of grid each field rowconstraints rowconstraints = new rowconstraints(); rowconstraints.setpercentheight(100d / 8); columnconstraints columnconstraints = new columnconstraints(); columnconstraints.setpercentwidth(100d / 8); (int = 0; < 8; i++) { board.getcolumnconstraints().add(columnconstraints); board.getrowconstraints().add(rowconstraints); } pane piecepane = new pane(); stackpane root = new stackpane(board, piecepane); // numberbinding boardsize = bindings.min(root.widthproperty(), root.heightproperty()); numberbinding boardsize = bindings.min(root.widthproperty(), root.heightproperty()); // board size should large possible @ min of parent sizes board.setprefsize(double.max_value, double.max_value); // same size piecepane piecepane.setprefsize(double.max_value, double.max_value); piecepane.maxwidthproperty().bind(boardsize); piecepane.maxheightproperty().bind(boardsize); scene scene = new scene(root,400,400); primarystage.setscene(scene); primarystage.show(); } catch(exception e) { e.printstacktrace(); } } public static void main(string[] args) { launch(args); } }
change:
stackpane root = new stackpane(board,piecepane); to:
stackpane root = new stackpane(piecepane,board); 🐞? because board behind piecepane couldn't receive events.
before using global variables col , row these variables had same value of 7 , 7. running loop variables changing value,but @ end had values 7 7 , here need use local variables col , row , need add modification:
(int = 0; < fields.length; i++) { int col = i; (int j = 0; j < fields[i].length; j++) { int row = j; fields[i][j].setonmouseclicked(e -> system.out.println("col:" + col + ", row" + row)); } }
Comments
Post a Comment