Calling a function in java by clicking on menu item - java

Am designing an application in java using netbeans, in which i need to call a "Save" function by clicking on the save item on the menu bar. Am able to call the function by having a button inside the main panel, but am not able to call the function when i click on the menu bar or any where else. How can this be done?

Start with How to Use Menus, then see this example of How to Use Actions.

First check if you have defined an actionPerformed() method for the menuitem from which you are trying to call the method. If not, then you can define an actionPerformed() method by right clicking on the menu item in the design frame and select "events>actionperformed" from the context menu. Also take care that the menu items objects should not be a static field.

Related

How to hide MenuItem in the toolbar in certain situations?

I have a form screen that is also an information screen, but in different states. When the user enters to create I want to hide the MenuItem that assigns to delete.
How to do this without breaking the app?
I'm trying to call it like this:
val menu = findViewById<MenuItem>(R.id.deleteBarra)
Here's the docs about it, but you basically want to do your menu setup in onPrepareOptionsMenu instead of onCreateOptionsMenu. onPrepareOptionsMenu gets called every time the menu needs to be displayed (instead of once, during setup).
So you can set a boolean or whatever to say whether the item should be shown, and call invalidateOptionsMenu() to redisplay it. In prepareOptionsMenu() you have access to the menu itself, so you can check that boolean and set the visibility on the appropriate item
Under what condition you want it to be hidden, you can add it under that condition.
menu.visibility = View.INVISIBLE
or
menu.visibility = View.GONE

Eclipse 4 RCP: Is there any ways to show the Menu items of Dynamic menu contribution through a handler of Direct Tool item?

I am trying to show a menu on clicking a Direct Tool item on an RCP application.
My Application.e4xmi is designed like this:
I am able to display a menu using,
On Clicking the drop down arrow,aboutToShow() gets executed and displays the menu elements.
#AboutToShow
public void aboutToShow(List<MMenuElement> items) {
//Displays a menu when the drop down link is clicked.
}
On Clicking the area within the marked square, the execute() method of the handler attached with the direct tool item gets executed, but I am not able to get the list of MMenuElement items.
Is there any ways to display the menu elements on clicking the Handled tool item on picture 2?

How to use the injectinputevent in android?

I want to make a touch on a button in a layout and also that touch is done by program using injectinputevent method, so that button is clicked even without touching it.
You can use the performClick method on that button.
Call this view's OnClickListener, if it is defined. Performs all normal actions associated with clicking: reporting accessibility event, playing a sound, etc.
You can call if from another method:
your_button.performClick();

Java Swing realtime change componente

Is there a possibility using Java Swing to add in a existing running Swing application a button as an example. The action of the button to have a call to an existing method in that application. Maybe using xml to define the structure of the button.
You can do it. When any action triggered you can create the button and set the listener for it.
But best practice is to create the button and listener at the build time. Here you need to hide/set visibility to false initially and when any action triggered, set the visibility to true.

Delete all usages of an actionlistener

Is there any way to delete an action listener off all components that have it attached?
Something like: Action.removeFromAll();
I have attached this Action to several different buttons and when any button is pushed it should no longer be possible to activate this action. So in the action performed of this action I want to delete it from all the buttons. Is this possible or will I just have to loop through all the buttons?
An easier work around would be to create an instance variable boolean shouldPerformAction = true on the action listener. And when you get the button pushed action, you set it to false. And in the actionPerformed() method, you check if the shouldPerformAction is false and return without doing any action.

Categories