aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Listeners execution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Listeners execution" Watch "Listeners execution" New topic
Author

Listeners execution

Kriti Garg
Ranch Hand

Joined: Sep 13, 2004
Posts: 50
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

Joined: Aug 14, 2001
Posts: 342
Hi Kriti,

Why are you adding more than one listener of each type to the component?


SCJP 1.4, www.gsi3d.org.uk
Kriti Garg
Ranch Hand

Joined: Sep 13, 2004
Posts: 50
i was just checking what will happen if i do that.
Pat Hays
Ranch Hand

Joined: Aug 20, 2004
Posts: 138
The reason is that Event methods are executed by thread.


Download Java GUI Builder, <a href="http://www.mars3000.com" target="_blank" rel="nofollow">http://www.mars3000.com</a>
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Listeners execution