I don't know if title is understandable. Whatever, I have some JMenuItems, and for these I've set up mouseListener.
mntmExtractPaleographyFeature.addMouseListener(this);
where mntmExtractPaleographyFeature is a JMenuitem and this is the class that implements MouseListener.
So I've added method for mouseListener like
@Override
public void mouseReleased(MouseEvent arg0) {
if(arg0.getSource()==mntmExtractPaleographyFeature) {
//Code Here
}
Now I have this JButton extractPaleographyB that does exact the same thing of the JMenuItem. I don't want to copy/paste code two times (also because It's not the only button/jmenuitem). I've tried with
extractPaleographyB.addMouseListener(mntmExtractPaleographyFeature.getMouseListeners()[1]);
but it doesn't work. Any idea?
Create an Action and use for both menu item and button
The piece of code from the tutorial
To create an Action object, you generally create a subclass of AbstractAction and then instantiate it. In your subclass, you must implement the actionPerformed method to react appropriately when the action event occurs. Here's an example of creating and instantiating an AbstractAction subclass: