java - How to tell what button is being pressed from 2 array's of buttons on two JFrames -


i have 2 array's of buttons shown in 2 jframes , need tell 1 being pressed, current solution detects button press first window pressed when click button on first window not able click button on second windows until program restarted.

my code is

 private void populatearray() {     for(int wy = 0;wy<8;wy++)     {         for(int wx =0; wx<8;wx++)         {             whitebutton[wx][wy] = new reversibutton();             whitebutton[wx][wy].addactionlistener(this);             if((wx == 3)&&( wy ==3))                 whitebutton[wx][wy].change(1);             else if((wx == 4)&&(wy == 3))                 whitebutton[wx][wy].change(2);             else if((wx == 3)&&(wy == 4))                 whitebutton[wx][wy].change(2);             else if((wx ==4)&&(wy == 4))                 whitebutton[wx][wy].change(1);         }     }     for(int = 0; by<8; by++)     {         for(int bx =0; bx<8;bx++)         {             blackbutton[bx][by] = new reversibutton();             blackbutton[bx][by].addactionlistener(this);             if((bx == 3)&&( ==3))                 blackbutton[bx][by].change(1);             else if((bx == 4)&&(by == 3))                 blackbutton[bx][by].change(2);             else if((bx == 3)&&(by == 4))                 blackbutton[bx][by].change(2);             else if((bx ==4)&&(by == 4))                 blackbutton[bx][by].change(1);         }     } } private void initgui() {     whiteframe.settitle("reversi white player");     blackframe.settitle("reversi black player");     whiteframe.setlayout(new borderlayout());     whitelabel.settext("white player - click place put piece");     whitegrid.setlayout(new gridlayout(8,8));     for(int wy = 0;wy<8;wy++)     {         for(int wx =0; wx<8;wx++)         {             whitegrid.add(whitebutton[wx][wy]);         }     }     whitebuttons.settext("greedy ai(play white)");     whiteframe.add(borderlayout.north,whitelabel);     whiteframe.add(borderlayout.center,whitegrid);     whiteframe.add(borderlayout.south,whitebuttons);     whiteframe.pack();     whiteframe.setvisible(true);     blackframe.setlayout(new borderlayout());     blacklabel.settext("black player - not turn");     blackgrid.setlayout(new gridlayout(8,8));     blackbutton = reverse.rotatearray(blackbutton);     for(int = 0; by<8; by++)     {         for(int bx =0; bx<8;bx++)         {             blackgrid.add(blackbutton[bx][by]);         }     }     blackbuttons.settext("greedy ai(play black)");     blackframe.add(borderlayout.north, blacklabel);     blackframe.add(borderlayout.center, blackgrid);     blackframe.add(borderlayout.south,blackbuttons);     blackframe.pack();     blackframe.setlocation(whiteframe.getx() + whiteframe.getwidth() + 10, whiteframe.gety());     blackframe.setvisible(true); }  @override     public void actionperformed(actionevent ae)     {         (int y = 0; y < gridsize; y++)         {             (int x = 0; x < gridsize; x++)             {                 if((ae.getsource()==whitebutton[x][y]))                 {                     if(whitebutton[x][y].is() ==0)                     {                         whitebutton[x][y].change(1);                         game.search(whitebutton, x, y, 1);                     }                 }                 if((ae.getsource() == blackbutton[x][y]))                 {                     if(blackbutton[x][y].is() == 0)                     {                         blackbutton[x][y].change(2);                         game.search(blackbutton, x, y, 2);                     }                 }             }         }     } 

how can detect button presses both jframes?

you define action command each button:

 whitebutton[wx][wy].setactioncommand(w + wx + "," + wy); 

and call getactioncommand retrieve string. alternatively, rig map

map<jbutton,buttondata> but2dat 

and store coordinates , current state in object of class buttondata.


Comments