java - Keeping a MouseListener always running -


i have constructor:

public board(final boolean[][] board) {     this.board = board;     height = board.length;     width = board[0].length;     setbackground(color.black);     button1 = new jbutton("run");     add(button1);     button1.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             isactive = !isactive;             button1.settext(isactive ? "pause" : "run");         }     });     button2 = new jbutton("random");     add(button2);     button2.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             setboard(randomboard());         }     });     button3 = new jbutton("clear");     add(button3);     button3.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             setboard(clearboard());         }     });     addmouselistener(new mouselistener() {         @override         public void mouseclicked(mouseevent e) {         }          @override         public void mouseentered(mouseevent e) {         }          @override         public void mouseexited(mouseevent e) {                  }          @override         public void mousepressed(mouseevent e) {             board[e.gety() / multiplier][e.getx() / multiplier] = !board[e.gety() / multiplier][e.getx() / multiplier];         }          @override         public void mousereleased(mouseevent e) {                }     }); } 

the actionlisteners 'listening'; mouselistenerstops 'listening' after click run (button1). why , how make mouselistener remain listening?

if it's use, have paintcomponent class:

@override public void paintcomponent(graphics g) {     super.paintcomponent(g);     (int = 0; < height; i++) {         (int j = 0; j < width; j++) {             g.setcolor(board[i][j] ? color.green : color.gray);             g.fillrect(j * multiplier, * multiplier, multiplier - 1, multiplier - 1);         }     }     if (isactive) {         timer.start();     }     else {         timer.stop();         repaint();     } } 

a mouselistener continue work long object added still alive , assuming haven't called removemouselistener() on it. program runs , changes data , such, behavior of code inside listener may change (e.g., flag set causing ignore call method), listener "always running" , methods will called.

(as mentioned in comment, problem has strange things doing in paintcomponent() method.)


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -