-2

I tried .equals() and ==, but nothing help. All labels I storage in ArrayList of my own class, which have JLabel.

How can I get the index of the label in ArrayList or something else?

Can my problem in using ArrayList? MouseListener


  private static MouseListener clicklvl1=new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                for (int i=0;i<shahtars.size();i+=1){
                     if (e.getSource()==shahtars.get(i).uiShahtar){
                         IDofClickedObject=i;
                    } 
                }
                if (IDofClickedObject!=-1){
                    if (counter == 0) {
                        shahtars.get(IDofClickedObject).uiShahtar.setIcon(new ImageIcon("E:\\aaa\\ShahtarLvL1(Clicked).png"));
                        counter = 1;
                    } else if (counter == 1) {
                        // uiShahtar.setIcon(new ImageIcon("E:\\aaa\\ShahtarLvL1.png"));
                        shahtars.get(IDofClickedObject).uiShahtar.setIcon(new ImageIcon("E:\\aaa\\ShahtarLvL1.png"));
                        counter = 0;
                    }
                }
                System.out.print("x "+shahtars.get(IDofClickedObject).uiShahtar.getX()+" y  "+shahtars.get(IDofClickedObject).uiShahtar.getY());

            }

My class


class  FillShahtar implements Cloneable {
    static JLabel uiShahtar;
    static int energy;
    static double power;
    static double speed;
    String name;

And the last

 FillShahtar(int chose) {
switch (chose){
    case 1:{

        plankaDown = 1;
        plankaUp = 11;

        int xRand = (int) ( 0+Math.random()*1000);
        int yRand =(int) (0+Math.random()*600);
         int counter=0;

        /////////////////////////debug/////////////////
        System.out.print(xRand+"   "+yRand+"\n");
        //////////////////////////////////////////////

        energy = (int) (plankaDown + Math.random() * plankaUp  );
        power = (int) (plankaDown + Math.random() * plankaUp  );
        speed = (int) (plankaDown + Math.random() * plankaUp  );


        uiShahtar = new JLabel();

        uiShahtar.setIcon(new ImageIcon("E:\\aaa\\ShahtarLvL1.png"));
        uiShahtar.setLayout(new FlowLayout());

        uiShahtar.setSize(50,50);
        uiShahtar.setLocation(xRand,yRand);

        uiShahtar.setVisible(true);
        uiShahtar.addMouseListener(clicklvl1);



//        mainPanel.add(shahtars.get(counter).uiShahtar);
        counter+=1;

        break;
    }

Clicked image must change the image, but change only last Label.

Hossein Nasr
  • 1,436
  • 2
  • 20
  • 40
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code data to replace the DB. 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). .. – Andrew Thompson Apr 25 '19 at 03:39
  • .. 3) I'd tend to use `JButton` and `ActionListener` over `JLabel` and `MouseListener` if only for the fact that the button with action listener will work by either mouse **or keyboard.** – Andrew Thompson Apr 25 '19 at 03:40

1 Answers1

1

Since JLabel inherits from swing's Component class, you can add a MouseListener (or MouseAdapter) to each of your clickable labels. Using this EventListener you can find the clicked label like this:

        JLabel l = new JLabel();
        l.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                JLabel clickedLabel = (JLabel) e.getComponent();
            }
        });

In order to get the index of the clicked label within the ArrayList, use the indexOf(Object) method provided by the ArrayList:

int index = list.indexOf(clickedLabel);
Brainsucker92
  • 329
  • 3
  • 13