skiabox
skiabox

Reputation: 3507

NetBeans : How to connect two components , so that they perform the same action?

I have created a JMenuItem and a JButton in a JForm. Is there an easy way to connect them so that they perform the same action, using Netbeans IDE? Thank you.

Upvotes: 0

Views: 215

Answers (2)

Razvan
Razvan

Reputation: 10103

They both implement ItemSelectable. So you can try to create a controller which implements ItemListener, implement its itemStateChanged() and add this listener to both the JMenuItem and the JButton (with addItemListener()).

Upvotes: 1

user330315
user330315

Reputation:

Use a javax.swing.Action to implement the logic (e.g. by extending javax.swing.AbstractAction) and then assign the action to the menu item and the button. Both have a constructor that takes an Action as the parameter.

Enabling/Disable the action will then also enable/disable the button and the menu item.

See the Swing tutorial's chapter about using actions for more details

Upvotes: 1

Related Questions