Action Demo : Actions « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing JFC » ActionsScreenshots 
Action Demo
Action Demo

/* From http://java.sun.com/docs/books/tutorial/index.html */

/*
 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * -Redistribution of source code must retain the above copyright notice, this
 *  list of conditions and the following disclaimer.
 *
 * -Redistribution in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */

/* Uses actions with a tool bar and a menu. */

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

public class ActionDemo extends JFrame {
  protected JTextArea textArea;

  protected String newline = "\n";

  protected Action leftAction;

  protected Action middleAction;

  protected Action rightAction;

  public ActionDemo() {
    //Do frame stuff.
    super("ActionDemo");
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    //Create the toolbar and menu.
    JToolBar toolBar = new JToolBar();
    JMenu mainMenu = new JMenu("Menu");
    createActionComponents(toolBar, mainMenu);

    //Create the text area used for output.
    textArea = new JTextArea(530);
    JScrollPane scrollPane = new JScrollPane(textArea);

    //Lay out the content pane.
    JPanel contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout());
    contentPane.setPreferredSize(new Dimension(400150));
    contentPane.add(toolBar, BorderLayout.SOUTH);
    contentPane.add(scrollPane, BorderLayout.CENTER);
    setContentPane(contentPane);

    //Set up the menu bar.
    JMenuBar mb = new JMenuBar();
    mb.add(mainMenu);
    mb.add(createAbleMenu());
    setJMenuBar(mb);
  }

  protected void createActionComponents(JToolBar toolBar, JMenu mainMenu) {
    JButton button = null;
    JMenuItem menuItem = null;

    //first button and menu item
    leftAction = new AbstractAction("Go left"new ImageIcon(
        "images/left.gif")) {
      public void actionPerformed(ActionEvent e) {
        displayResult("Action for first button/menu item", e);
      }
    };
    button = toolBar.add(leftAction);
    button.setText("")//an icon-only button
    button.setToolTipText("This is the left button");
    menuItem = mainMenu.add(leftAction);
    menuItem.setIcon(null)//arbitrarily chose not to use icon in menu

    //second button and menu item
    middleAction = new AbstractAction("Do something"new ImageIcon(
        "images/middle.gif")) {
      public void actionPerformed(ActionEvent e) {
        displayResult("Action for second button/menu item", e);
      }
    };
    button = toolBar.add(middleAction);
    button.setText("");
    button.setToolTipText("This is the middle button");
    menuItem = mainMenu.add(middleAction);
    menuItem.setIcon(null)//arbitrarily chose not to use icon in menu

    //third button and menu item
    rightAction = new AbstractAction("Go right"new ImageIcon(
        "images/right.gif")) {
      public void actionPerformed(ActionEvent e) {
        displayResult("Action for third button/menu item", e);
      }
    };
    button = toolBar.add(rightAction);
    button.setText("");
    button.setToolTipText("This is the right button");
    menuItem = mainMenu.add(rightAction);
    menuItem.setIcon(null)//arbitrarily chose not to use icon in menu
  }

  protected JMenu createAbleMenu() {
    JMenu ableMenu = new JMenu("Action State");
    JCheckBoxMenuItem cbmi = null;

    cbmi = new JCheckBoxMenuItem("First action enabled");
    cbmi.setSelected(true);
    cbmi.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        JCheckBoxMenuItem mi = (JCheckBoxMenuItem) (e.getSource());
        boolean selected = (e.getStateChange() == ItemEvent.SELECTED);
        leftAction.setEnabled(selected);
      }
    });
    ableMenu.add(cbmi);

    cbmi = new JCheckBoxMenuItem("Second action enabled");
    cbmi.setSelected(true);
    cbmi.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        JCheckBoxMenuItem mi = (JCheckBoxMenuItem) (e.getSource());
        boolean selected = (e.getStateChange() == ItemEvent.SELECTED);
        middleAction.setEnabled(selected);
      }
    });
    ableMenu.add(cbmi);

    cbmi = new JCheckBoxMenuItem("Third action enabled");
    cbmi.setSelected(true);
    cbmi.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        JCheckBoxMenuItem mi = (JCheckBoxMenuItem) (e.getSource());
        boolean selected = (e.getStateChange() == ItemEvent.SELECTED);
        rightAction.setEnabled(selected);
      }
    });
    ableMenu.add(cbmi);

    return ableMenu;
  }

  protected void displayResult(String actionDescription, ActionEvent e) {
    String s = ("Action event detected by: " + actionDescription + newline
        "    Event source: " + e.getSource() + newline);
    textArea.append(s);
  }

  public static void main(String[] args) {
    ActionDemo frame = new ActionDemo();
    frame.pack();
    frame.setVisible(true);
  }
}

           
       
Related examples in the same category
1. UseActions: MenuUseActions: Menu
ww__w_.___j__a__va__2___s.__c__om__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.