• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Listeners execution

 
Kriti Garg
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
i was just checking what will happen if i do that.
 
Pat Hays
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is that Event methods are executed by thread.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic