How to fire an event programmatically ?

F

Francesco M.

Hi all,
I have a Swing application which delivers its options via JMenuItem.
Of course there's a listener associated on every menuItem:

menuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
doBusinessOperation();
}
});

Now the matter is, that I'm in need to fire the action
(actionPerfomed)
programmatically, for example when I display an error popup I should
fire an event which calls actionPerformed.

How would you do it ?
Thanks a lot in advance
Francesco
 
K

Karsten Baumgarten

Francesco said:
Hi all,
I have a Swing application which delivers its options via JMenuItem.
Of course there's a listener associated on every menuItem:

menuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
doBusinessOperation();
}
});

Now the matter is, that I'm in need to fire the action
(actionPerfomed)
programmatically, for example when I display an error popup I should
fire an event which calls actionPerformed.

How would you do it ?
Thanks a lot in advance
Francesco

You have several options. For example:

Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(AWTEvent e)
 
T

Thomas Weidenfeller

Francesco said:
Now the matter is, that I'm in need to fire the action
(actionPerfomed)
programmatically, for example when I display an error popup I should
fire an event which calls actionPerformed.

First of all, I can't follow your reasoning. Are you trying to re-use
some action code in different situations? If this is the case, you might
want to factor that code out into a separate method.

Regarding firing events manually: Since the listeners are just classes
with methods, you can simply invoke the methods yourself if you keep
references to the listener objects.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top