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

51 views Asked by At

I have this piece of code in a 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 the variables (fields of the class) are

JLabel[] switches = new JLabel[3];      //I've initialized each index
boolean[] switchstate = new boolean[3]; //Indices Initialized to '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 to shorten that piece of code and so, I 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 the two 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 the shortened code gives me an error:

error: local variable i is accessed from within inner class; needs to be declared final

But if I declare i as final, I cannot use i++ as it gives

error: cannot assign a value to final variable i

How do I avoid this problem?

1

There are 1 answers

0
MD Sayem Ahmed On BEST ANSWER

Use another variable -

for(int i = 0; i < 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 this way you can capture the value of i inside your mouseClicked method, rather than capturing the variable itself (which unfortunately Java does not allow yet).