java - Shortening a piece of code gives an error; How do I solve this issue? -


this question has answer here:

i have piece of code in method:

switches[0].addmouselistener(new mouseadapter(){         public void mouseclicked(mouseevent e)         {             if(switchstate[0] == false)             {                 if((e.getx() >= offbuttonleft && e.getx() <= offbuttonright) && (e.gety() >= offbuttontop && e.gety() <= offbuttondown))                     switchstate[0] = true;             }else             {                 if((e.getx() >= onbuttonleft && e.getx() <= onbuttonright) && (e.gety() >= onbuttontop && e.gety() <= onbuttondown))                     switchstate[0] = false;             }              paintstuff();         }     });  switches[1].addmouselistener(new mouseadapter(){         public void mouseclicked(mouseevent e)         {             if(switchstate[1] == false)             {                 if((e.getx() >= offbuttonleft && e.getx() <= offbuttonright) && (e.gety() >= offbuttontop && e.gety() <= offbuttondown))                     switchstate[1] = true;             }else             {                 if((e.getx() >= onbuttonleft && e.getx() <= onbuttonright) && (e.gety() >= onbuttontop && e.gety() <= onbuttondown))                     switchstate[1] = false;             }              paintstuff();         }     });  switches[2].addmouselistener(new mouseadapter(){         public void mouseclicked(mouseevent e)         {             if(switchstate[2] == false)             {                 if((e.getx() >= offbuttonleft && e.getx() <= offbuttonright) && (e.gety() >= offbuttontop && e.gety() <= offbuttondown))                     switchstate[2] = true;             }else             {                 if((e.getx() >= onbuttonleft && e.getx() <= onbuttonright) && (e.gety() >= onbuttontop && e.gety() <= onbuttondown))                     switchstate[2] = false;             }              paintstuff();         }     }); 

where variables (fields of class) are

jlabel[] switches = new jlabel[3];      //i've initialized each index boolean[] switchstate = new boolean[3]; //indices initialized 'false' 

and

final static int offbuttontop   = 75; final static int offbuttonleft  = 30; final static int offbuttonright = 65; final static int offbuttondown  = 115;  final static int onbuttontop   = 35; final static int onbuttonleft  = 25; final static int onbuttonright = 60; final static int onbuttondown  = 75; 

i want shorten piece of code , so, did

for(final int i=0; i<switchstate.length; i++)     switches[i].addmouselistener(new mouseadapter(){         public void mouseclicked(mouseevent e)         {             if(switchstate[i])             {                 if(on_rectangle.contains(e.getx(), e.gety()))                     switchstate[i] = false;             }             else             {                 if(off_rectangle.contains(e.getx(), e.gety()))                     switchstate[i] = true;             }         }     }); 

with 2 new variables being

final static rectangle off_rectangle = new rectangle(30, 75, 35, 40);  final static rectangle on_rectangle  = new rectangle(25, 35, 35, 30); 

but shortened code gives me error:

error: local variable accessed within inner class; needs declared final 

but if declare i final, cannot use i++ gives

error: cannot assign value final variable 

how avoid problem?

use variable -

for(int = 0; < switchstate.length; i++) {     final int j = i;     switches[i].addmouselistener(new mouseadapter(){         public void mouseclicked(mouseevent e)         {             if(switchstate[j])             {                 if(on_rectangle.contains(e.getx(), e.gety()))                     switchstate[j] = false;             }             else             {                 if(off_rectangle.contains(e.getx(), e.gety()))                     switchstate[j] = true;             }              paintstuff();         }     }); } 

in way can capture value of i inside mouseclicked method, rather capturing variable (which unfortunately java not allow yet).


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -