Swing in java describes a graphical user interface for applications. Swing abstractaction method is an useful mechanism which is used to get and set methods for action listeners and it can be shared and co-ordinated. You need to specify the what action should be action performed. Here, this program focus on the creation of an action for Menu.
ActionExample.java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ActionExample extends JPanel {
public JMenuBar menuBar;
public JToolBar toolBar;
public ActionExample() {
super(true);
menuBar = new JMenuBar();
menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
JMenu menu = new JMenu("Menu");
menuBar.add(menu);
toolBar = new JToolBar();
toolBar.setBorder(new EtchedBorder());
SampleAction exampleAction = new SampleAction("Clicked",
new ImageIcon("action.gif"));
menu.add(exampleAction);
toolBar.add(exampleAction);
}
class SampleAction extends AbstractAction {
public SampleAction(String text, Icon icon) {
super(text,icon);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Action [" e.getActionCommand() "] performed!");
}
}
public static void main(String s[]) {
ActionExample example = new ActionExample();
JFrame frame = new JFrame("Action Example");
frame.setJMenuBar(example.menuBar);
frame.getContentPane().add(example.toolBar, BorderLayout.NORTH);
frame.setSize(200,200);
frame.setVisible(true);
}
}