java - SWT Button Grid -
it's problem it's annoying me 3 days now. have rewrite ui of little tictactoe(gomoku of n x n) game. problem when created swing gui , made new class inherits jbutton properties , added int rows , int columns. cannot swt(no inheritance). there way me add values of , j button.
here example in swing:
for (int = 0; < rows; i++) { (int j = 0; j < cols; j++) { final myjbutton button = new myjbutton(i, j); button.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { moveresult move = game.move(button.getrow(), button.getcol()); switch (move) { case validmove: button.setbackground(game.getcurrentplayer().getcolor()); game.changeplayer(); break; } } } } } } i give , j game class give table clas check move.
if (table.getelement(x, y) != piecetype.none) return moveresult.invalidmove; private piecetype[][] table; is there way same in swt, indication welcomed .
this made
buttonpanel = new composite(shell, swt.none); buttonpanel.setlayout(new org.eclipse.swt.layout.gridlayout(cols, true)); buttontable = new button[rows][cols]; (int = 0; < rows; ++i){ (int j = 0; j < cols; ++j) { griddata.heighthint = 45; griddata.widthhint = 45; button button = new button(buttonpanel, swt.push); button.setlayoutdata(griddata); buttontable[i][j] = button; buttontable[i][j].addselectionlistener(new buttselectionlistener()); // buttonpanel.pack(); } }
i see 2 solutions :
- use button's setdata method (defined in widget superclass) associate object containing x , y (you'll found data in event object provided listener)
- use different listeners each button
in case, first solution seems natural one. means creating class holding x , y (let's call cell), , doing
button.setdata(new cell(i, j)); and in listener using
game.move(e.data.x, e.data.y);
Comments
Post a Comment