java - JLabel isnt detected with MouseClick on JPanel -
im using netbeans , i'm dynamically adding labels shapes inside them jpanel. however, added mouselistener panel detects actions when mouse isn't on jlabel? there way include jlabel inside jpanel in mouselistener without adding listeners each individual jlabel?
private void jpanel2mouseclicked(java.awt.event.mouseevent evt) { for(int = 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 create jlabels doing usual 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();
try making sure function even being called sticking print
line somewhere inside of it. this:
private void jpanel2mouseclicked(java.awt.event.mouseevent evt) { system.out.println("this method called"); for(int = 0; < 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(); } } }
Comments
Post a Comment