Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-12-2008, 12:00 AM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
ActionEvent example
Presented below is an interesting example in whihc a JMenu, JToolbar and JButtons on a JFrame are sharing common ActionEvent handlers.

Code:
public class ShareAction extends JFrame { OpenAction openAction = new OpenAction(); SaveAction saveAction = new SaveAction(); QuitAction quitAction = new QuitAction(); public static void main(String argv[]) { new ShareAction().setVisible(true); } ShareAction() { createMenu(); createToolBar(); createButtons(); pack(); // deal closing via the upper right "X" by redirecting to // quitAction addWindowListener (new WindowAdapter() { public void windowClosing(WindowEvent e) { ActionEvent ae = new ActionEvent (quitAction, ActionEvent.ACTION_PERFORMED, "bebye"); JFrame j = (JFrame)e.getSource(); j.dispatchEvent(ae); } } ); } public Dimension getPreferredSize() { return new Dimension(300,300); } private void createMenu(){ JMenuBar mb = new JMenuBar(); JMenu file = new JMenu("File"); mb.add(file); file.add(openAction); file.add(saveAction); file.add(quitAction); setJMenuBar(mb); } private void createToolBar() { JToolBar bar = new JToolBar(); bar.add(openAction); bar.add(saveAction); bar.add(quitAction); getContentPane().add(bar, "North"); } private void createButtons() { JPanel j = new JPanel(); JButton b1 = new JButton("Open"); JButton b2 = new JButton("Save"); JButton b3 = new JButton("Quit"); b1.addActionListener(openAction); b2.addActionListener(saveAction); b3.addActionListener(quitAction); j.add(b1); j.add(b2); j.add(b3); getContentPane().add(j, "East"); } } class OpenAction extends AbstractAction { public OpenAction() { super("Open", new ImageIcon("open.gif")); } public void actionPerformed(ActionEvent e) { System.out.println("Open action"); } } class SaveAction extends AbstractAction { public SaveAction() { super("Save", new ImageIcon("save.gif")); } public void actionPerformed(ActionEvent e) { System.out.println("Save action"); } } class QuitAction extends AbstractAction { public QuitAction() { super("Quit", new ImageIcon("quit.gif")); } public void actionPerformed(ActionEvent e) { System.out.println("Quit action"); System.out.println("Bye."); System.exit(0); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Actionevent problem Cymro New To Java 3 04-04-2008 08:11 AM


All times are GMT +3. The time now is 11:13 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org