java - Multiple JLables into a single addMouseListener -


i got 3 jlables, , added functionality l3 of when clicked, set jlabel text value store array (ans.get(0,0))

jlabel l1 = new jlabel("obtener x1");  jlabel l2 = new jlabel("obtener x2");  jlabel l3 = new jlabel("obtener x3"); l3.setbounds(30, 180, 100, 25); l3.addmouselistener(new mouselistener() {    @override   public void mouseclicked(mouseevent e)    {   l3.settext("x1 = "+ ans.get(0, 0));   }   @override   public void mouseentered(mouseevent e) {   //mouseentered, mousepressed, mouserelease overriden methods.... 

the question here is, posible implementent rest of jlabels (l1, l2) single addmouselistener (same mouselistener belonging jlabel l3) ?

create local variable mouselistener , assign instance it. add mouselistener each jlables.

jlabel l1 = new jlabel("obtener x1"); jlabel l2 = new jlabel("obtener x2"); jlabel l3 = new jlabel("obtener x3");  mouselistener ml = new mouselistener() {...}  l1.addmouselistener(ml); l2.addmouselistener(ml); l3.addmouselistener(ml); 

then each jlable share same mouselistener object.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -