0

Im using netbeans and I'm dynamically adding labels with shapes inside them to my jpanel. However, i added a mouselistener to my panel but it only detects actions when the mouse isn't over the jlabel? Is there a way to include the jLabel inside my jPanel in my mouseListener without adding listeners to each individual jLabel?

private void jPanel2MouseClicked(java.awt.event.MouseEvent evt) {                                     
    for(int a = 0; a< states.size(); a++) {
        if(states.get(a).contains(evt.getPoint())) {
            if(states.get(a).getIcon() == rectangleIcon) {
                if(yellow == true) {
                    states.get(a).setIcon(yellow2Icon);
                    yellow = false;
                } else if (yellow != true){
                    states.get(a).setIcon(yellowIcon); 
                    jPanel2.revalidate();
                    jPanel2.repaint();
                    yellow = true;
                }
            } else {
                states.get(a).setIcon(rectangleIcon);
                yellow = false;
            }
            x2 = evt.getPoint();
        } 
    }
}    

and i create my jLabels by doing the usual to my jpanel

JLabel rectanglelLabel = new JLabel(rectangleIcon);
states.add(rectanglelLabel);

rectanglelLabel.setText("State " + counter);
rectanglelLabel.setHorizontalTextPosition(JLabel.CENTER);
rectanglelLabel.setBounds((counter*100)%500,((counter/3)*100)%500,50,50);
counter++;
jPanel2.add(rectanglelLabel);
jPanel2.revalidate();
jPanel2.repaint();
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
zarir
  • 29
  • 6
  • 4
    A JLabel shouldn't steal a mouse click from the underlying JPanel *unless* the JLabel also has a MouseListener attached to it -- does it? Consider creating and posting your [mcve]. – Hovercraft Full Of Eels Jan 16 '16 at 16:14
  • Wow you were right, i wrote a mouselistener when i first initialzed it and i never knew wow thanks – zarir Jan 16 '16 at 16:28
  • Possible duplicate of [this](http://stackoverflow.com/q/34823601/230513). – trashgod Jan 16 '16 at 23:30

1 Answers1

0

Try making sure that this function is even being called by sticking a print line somewhere inside of it. Like this:

private void jPanel2MouseClicked(java.awt.event.MouseEvent evt) {
    System.out.println("this method was called");                                 
    for(int a = 0; a < states.size(); a++) {
        if (states.get(a).contains(evt.getPoint())) {
            if (states.get(a).getIcon() == rectangleIcon) {
                if (yellow)
                    states.get(a).setIcon(yellow2Icon);
                else {
                    states.get(a).setIcon(yellowIcon); 
                    jPanel2.revalidate();
                    jPanel2.repaint();
                }
                yellow = !yellow;
            } else {
                states.get(a).setIcon(rectangleIcon);
                yellow = false;
            }
            x2 = evt.getPoint();
        } 
    }
}    
John S.
  • 626
  • 1
  • 4
  • 16