Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to fire an event programmatically ?  RSS feed

 
Francesco Marchioni
author
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


D.
 
Francesco Marchioni
author
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
original solution but unfortunately not possible.
Besides that the business operation is not a one-line operation
but -most important- I don't want to give access to business operations
from other points than the actionPerformed method.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want to give access to business operations
from other points than the actionPerformed method.


but they will be accessible anyway from the sane class unless they are methods on the action listener class themselves, which in that case you have need for another class

Besides that the business operation is not a one-line operation


Extract those lines into a method.

Anyhoo, if you really want to programmatically fire an event, you can use menuItem.doClick()

hth, D.
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Francesco,

You may try this: myJMenuItem.doClick(); /* Simulate a click on the menu item */

Hope that helps,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!