How to trigger a method with the JMenu bean

Hello,

the following lines will create a menu bar with two menu items. The form consists of two bean elements:

  • JMenu
  • JMenuBar
elements.jmenu.add('Menu Item 1');
elements.jmenu.add('Menu Item 2');

elements.jmenu_bar.add(elements.jmenu);
elements.jmenu_bar.updateUI();

Now the question is, how to trigger a method within Servoy after the user has selected one of these menu items.

Thanks,

Christoph

I am very interested in that, too. I can construct a nice looking menu but have no clue how to trigger a method if the user chooses an entry. I have the feeling that this is not possible. Does anyone have an idea?

Thanks
Patrick

What do you do with the menubar when you have created it? Because it is not in the place where you want it to be?

I think Paul Bakker has a menu bar implementation that lets’ you change everything and also call methods with it when you press/select a item.

Somehow I doubt it is possible to make javabeans trigger any Servoy method due to the very nature of the thing.
You can talk TO it…but it doesn’t talk back (with Javascript that is..).

Unless you use the bean to create a Servoy plugin that provides for the callbacks.

But I am sure Jan Blok or others can tell you for sure.

To Johan: You are right, we still want that real menu bar! :lol:

The jmenu comes in handy for example when creating a menu let’s say in the header of a list. There I could offer things that only apply to the items in the list. Apple does menus like this for example with their “action” menu. In an OS X finder window you can access file/folder related functions in such an action menu. Basically we are talking about a replacement/enhancement for a contextual menu that is visible to the user.

My impression is the same as Robert’s though: you can talk to the bean but the bean won’t talk to you? Is that right? All I see is a listener in the bean, but I don’t really see how I can put this to work.

Thanks!
Patrick

a javabean or servoy plugin can talk back to servoy. But then it does need to know about servoy methods (that plugins do anyway) So if you make a bean then you can give that bean a method where you can give a javascript method as a parameter. Then that bean can call that javascript method on a specific time or event. That is how the Scheduler Plugin works.

So if you had a MenuBarPlugin (that is being build by paul bakker i believe) you should be able to attach a special menu that can call back any javascript method you want.

Those listeners interfaces are pure java. That you can also do in javascript but this is for the very advanced programmeurs…

Yes, I’m working on a MenuBar Customization plugin.

Shortly after the release of Servoy 2.1.2 Beta/RC I will post a beta version of the plugin on the forum.

The plugin will let you:

  • Add, Remove, Hide, Enable/Disable and Rename Menu(Item)s, both on the MenuBar and inside Menus
  • MenuItems can be Regular, Checkboxes or RadioButtons
  • Add Icons to Menu(Item)s
  • Add KeyBoard shortcuts to Menu(Item)s
  • Reset to the default Servoy MenuBar
  • Create a MenuBar on a FormInDialog
  • Trigger specific global methods when a MenuItem is clicked
  • Programatically click a menuItem
  • Hide/Show the entire MenuBar

The content of the MenuBar can be modifier at runtime by methods.

Paul

Hello Paul,

that sounds really great! As far as I understand this, though, it will be “limited” to the main menu bar of a window, right? The question here was related to a menu that can be placed anywhere.

Two more questions:

  1. Will this plugin be free or are you planning to sell it (if yes, how much will it more or less be)?
  2. Since you already offer all required features for a custom menu bar, Servoy will not offer that themselves?

I am excited to see what wwe can do with your plugin! It does sound really great!

Thanks!
Patrick

Patrick,

for now it will be limited to the Main MenuBar and MenuBars on forms in Dialogs.

I’ll have a look if i can add the feature requested here in the future.

The plugin will not be free of charge (spent a lot of time getting it to work properly :) ). The exact prizing is yet to be determined, but will be a lot less then comparable products in the FM world…

as for what Servoy will include or not: I do not have influence on that, but from what I hear is that if there is a plugin available that does the trick, they will work on other requests first.

Paul

I am looking forward to have a look at it! If it does what you say you have a customer.

Patrick

patrick:
… If it does what you say you have a customer.

And another… 8)

Am I correct in thinking this plugin will allow methods to be assigned to keystrokes and/or a combination of keystrokes? There are a few basic methods that I would really like to be able to assign keystrokes to instead of having the user have to hit a button for. If your plugin will allow that I too will be in the queue! Any update as to when it will be available?

Yes, you are right, this will aslo be possible with this plugin.

Plan is to post the first release candidate this week on the forum.

Paul

jcompagner:
…So if you make a bean then you can give that bean a method where you can give a javascript method as a parameter. Then that bean can call that javascript method on a specific time or event…

Let me make sure I got this right. I assume the method you are talking about would run on any change to the bean UI, for example movement of the slider in jslider. This would then run some javascript passed as a parameter and this would have access to Servoy methods??? Interesting interface. What would this javascript passed as a parameter look like?

Now if Servoy can read and write properties of a bean and execute its methods, can it also set a change listener method of the bean to do the above passing the javascript needed along the way?

What’s it take to do this? I’m not a java coder, but have friends who could help but they are clueless on servoy. Any guidance appreciated.

yes this all is possible
If you make the bean/plugin in java then you can let java call a javascript method on any kind of event
Please let youre friends look at the scheduler plugin. The src is included

Check out http://forum.servoy.com/viewtopic.php?t=3680&highlight=

for the plugin that allows to customize your menus.

also possible: place a JMenuBar bean on your form and create a menu on it, using this plugin. this way you can create a branching popup.

paul

yes this all is possible
If you make the bean/plugin in java then you can let java call a javascript method on any kind of event
Please let youre friends look at the scheduler plugin. The src is included

Please could someone show me a really simple example of how to call a Servoy method from a Java Bean? I’ve looked at the Scheduler example and just can’t see how its done…

I’m not really a java programmer, but have managed to create a java bean and include it Servoy, just need to do this bit.

Thanks,

Nick

Hi Nick, it is a combination of different bits and pieces. It is not at all that difficult but also not too simple to ‘write it down’. The easiest way imho is that scheduler plugin…

I think I saw that the plugin API in Servoy 3.0 eta’s now includes a function to execute a method. If I saw correctly, this will probably simplify things a lot.

Paul

nick:
Please could someone show me a really simple example of how to call a Servoy method from a Java Bean? I’ve looked at the Scheduler example and just can’t see how its done…

I’m not really a java programmer, but have managed to create a java bean and include it Servoy, just need to do this bit.

Thanks,

Nick

Hi Nick,

The following is a bit of code that when called from your bean will invoke a method in Javascript within Servoy:

// ... inside your Java bean class.
private org.mozilla.javascript.Function jsClickyFunction;


/**
 * This has a js_ prefix so that the Servoy Javascript can see it and 
 * call it to pass in the name of your callback method.
 * Ex: (In Servoy Javascript)
 * elements.mybean.setClickyCallback(myCallbackMethod);
 */
public void js_setClickyCallback(Function f) {
        this.jsClickyFunction = f;
}

/**
 * When something is clicked in your bean, or whenever you want to
 * call Javascript.
 * Remember, jsClickyFunction is set to the value of your javascript
 * method (called by javascript above).
 */
private void somethingWasClicked() {
	if (jsClickyFunction == null) return;

	Context context = Context.enter();
	try {

		// This is where the javascript method is actually invoked.

		jsClickyFunction.call(
				context,
				jsClickyFunction.getParentScope(),
				jsClickyFunction.getParentScope(),
				new Object[] {
					"first string parameter passed to Javascript.",
					"The objects in this array will be passed",
					"to your javascript method as arguments...",
					"eg. var arg1 = arguments[0];",
					"eg. var arg2 = arguments[1];",
					"... ",
					new Integer(25)
                                });
	}
	catch (JavaScriptException jse) {
		jse.printStackTrace();
	}
	finally {
		// Important to ALWAYS include this finally block!
		Context.exit();
	}
}

This is the basic structure of calling a javascript method. If you don’t want to pass any parameters to the javascript method, just leave the Object array empty… (new Object {}).

Hope this helps.