How to return the index of a java ARRAY

Asked

Viewed 45 times

0

I have a little problem that I believe is logical but I can not solve.

Let’s assume that, I wrote a little algorithm that creates 10 panels and puts them next to each other, as shown below.

JPanel painel[]= new JPanel[10];
for(i = 0; i < 10; I++)
{
    painel[i].setSize(10, 10);
    painel[i].setLocation(2 * i, 0);
    painel[i].setBackground(Color.black);
}

Doubt now enters, assuming I want these same panels to be clickable (from now on, correct me if there’s anything wrong), then I add one more part inside the loop, which leaves the code like this:

JPanel painel[]= new JPanel[10];
for(i = 0; i < 10; I++)
{
    painel[i].setSize(10, 10);
    painel[i].setLocation(2 * i, 0);
    painel[i].setBackground(Color.black);
    painel[i].addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e)
        {

        }
    });
}

Now the problem arises, my intention is that when any of the panels created is clicked, it should return the index of the matrix in which it is located, only I do not know how to do this within the method, much less if what I have done so far is right, if anyone can help me, I will be eternally grateful.

  • What you mean by "return" the index when the panel is clicked?

  • Print the value for example.

1 answer

0

public void mouseClicked(MouseEvent e)
        {
        /*escrever o indice "i" aqui não funcionaria?
         uma ideia seria definir o nome das jpanes e escrevelas nesse evento de click.
        */
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.