Action Demo : Actions « Swing JFC « Java






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(5, 30);
    JScrollPane scrollPane = new JScrollPane(textArea);

    //Lay out the content pane.
    JPanel contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout());
    contentPane.setPreferredSize(new Dimension(400, 150));
    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.Creating an Action
2.Enabling an Action
3.UseActions: MenuUseActions: Menu
4.Action events are fired by subclasses of AbstractButton and includes buttons, checkboxes, and menus.
5.Listing the Actions in a Component
6.Sharing an InputMap or an ActionMap Between Two Components
7.Apply special filter to a JTextField
8.Listing the Key Bindings in a Component
9.A text component has an action map with actions and keymaps with actions
10.extends AbstractAction to create your won actionextends AbstractAction to create your won action