Using an Action
This example creates an Action for both a menu item and a toolbar, displaying both components and allowing the user to click on either one. When the components are clicked, the actionPerformed( ) method of the action is called.
Here the code :
// 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);
// Create a menu bar and give it a bevel border.
menuBar = new JMenuBar( );
menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
// Create a menu and add it to the menu bar.
JMenu menu = new JMenu(”Menu”);
menuBar.add(menu);
// Create a toolbar and give it an etched border.
toolBar = new JToolBar( );
toolBar.setBorder(new EtchedBorder( ));
// Instantiate a sample action with the NAME property of “Download” and the
// appropriate SMALL_ICON property.
SampleAction exampleAction = new SampleAction(”Download”,
new ImageIcon(”action.gif”));
// Finally, add the sample action to the menu and the toolbar. These methods
// are no longer preferred:
// menu.add(exampleAction);
// toolBar.add(exampleAction);
// Instead, you should create actual menu items and buttons:
JMenuItem exampleItem = new JMenuItem(exampleAction);
JButton exampleButton = new JButton(exampleAction);
menu.add(exampleItem);
toolBar.add(exampleButton);
}
class SampleAction extends AbstractAction {
// This is our sample action. It must have an actionPerformed( ) method, which
// is called when the action should be invoked.
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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(example.menuBar);
frame.getContentPane( ).add(example.toolBar, BorderLayout.NORTH);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Related Websites -
How to Revitalize Your Woodwork Few people know that proper care of woodwork requires much more than an occasional dusting. Your woodwork was once made up of a living thing, and without the proper care, it's going to look quite dead. However, it is possible to revitalize your wood and keep it strong and healthy...... -
Quick and Inexpensive Kitchen Updates Everyone knows that the heart of the home can be found within the kitchen. As a result, you need to make a top priority out of turning your kitchen into a beautiful and comfortable space. Believe it or not, it does not need to be taxing or expensive for you...... -
The Personal Touch - Why Companies Need to Blog As corporate blogging becomes more popular, many companies are wondering if this is something that is right for them. There are many benefits that can be achieved by having a corporate blog, such as increased sales, better brand recognition and SEO, but none are as important as creating that personal......


Spanking Storyboard
Thursday, 17th December 2009 at 22:34
now I stay in touch!