• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Listeners execution

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

did any body help me about this,

i have a JButton named editBtn,when i add actionListerner to it ....

editBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {

System.out.println("in first :"+ae.getWhen()+".."+ae.getID());
}


});
editBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("in second :"+ae.getWhen()+".."+ae.getID());
}


});

editBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("in third :"+ae.getWhen()+".."+ae.getID());
}

The output is...

in third
in second
in first

..... from bottom to top always

but when i add Mouse or Key Listener ,like

editBtn.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed 1");
}
public void mouseReleased(MouseEvent e) {
System.out.println("MouseReleased 1");
}


});

editBtn.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed 2");
}
public void mouseReleased(MouseEvent e) {
System.out.println("MouseReleased 2");
}


});

it is executed from top to bottom

can anybody tell Why?

Thanks
Kriti Garg
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kriti,

Why are you adding more than one listener of each type to the component?
 
Kriti Garg
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i was just checking what will happen if i do that.
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is that Event methods are executed by thread.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no guarantee in which order event listeners will be invoked. The order may change with each release of the JDK, or may vary by platform. It's a mistake to write listeners that assume they'll be called in a particular order.
 
reply
    Bookmark Topic Watch Topic
  • New Topic