event

Action Class example

With this example we shall show you how to work with Action class in Java. In many ways the use of the Action class is pretty much the same as the use of the ActionListener interface.

With Action class you can monitor a number of events in a component and customize the behavior of your application when a specific event occurs. The action class is very flexible because you can construct a number of classes that extend AbstractAction and customize your own Action and your own events you wish to monitor.

Basically, all you have to do in order to work with Action class is:

  • Create a number of classes that extend the AbstractAction class.
  • Override the actionPerformed to customize the handling of a specific event.
  • Use the putValue method to set the specific values of different parameters to further customize the events you want to handle.

Let’s see the code snippets that follow:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class Main extends JFrame {

    private int gotoChannel = 5;

    private JLabel channelLabel = new JLabel();

    public static final int Min_Channel = 2;

    private int curr_chanel = Min_Channel;

    private Action incrAction = new Increase();

    public static final int MAX_CHANNEL = 20;

    private Action decrAction = new Decrese();

    private GotoAction gotoAction = new GotoAction();

    private Action setGoToAction = new SetGoToAction();

    public class Increase extends AbstractAction {

  public Increase() {

putValue(NAME, "Increase");

putValue(SHORT_DESCRIPTION, "Increment the number");

putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_U));

  }

  @Override

  public void actionPerformed(ActionEvent ae) {

setChannel(curr_chanel + 1);

  }
    }

    public class Decrese extends AbstractAction {

  public Decrese() {

putValue(NAME, "Decrease");

putValue(SHORT_DESCRIPTION, "Decrement the number");

putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_D));

  }

  @Override

  public void actionPerformed(ActionEvent ae) {

setChannel(curr_chanel - 1);

  }
    }

    public class GotoAction extends AbstractAction {

  public GotoAction() {

putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_G));

update();

  }

  public void update() {

putValue(NAME, "Go to " + gotoChannel);

putValue(SHORT_DESCRIPTION, "Change to "

  + gotoChannel);

  }

  @Override

  public void actionPerformed(ActionEvent ae) {

setChannel(gotoChannel);

  }
    }

    public class SetGoToAction extends AbstractAction {

  public SetGoToAction() {

putValue(NAME, "Set 'Go to' number");

putValue(SHORT_DESCRIPTION,"Make current number the goto channel");

putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_S));

  }

  @Override

  public void actionPerformed(ActionEvent ae) {

gotoChannel = curr_chanel;

gotoAction.update();

setEnabled(false);

gotoAction.setEnabled(false);

  }
    }

    public Main() {

  super("");

  setChannel(curr_chanel); 

  channelLabel.setHorizontalAlignment(JLabel.CENTER);

  channelLabel.setFont(new Font("Serif", Font.PLAIN, 42));

  getContentPane().add(channelLabel, BorderLayout.NORTH);

  JPanel buttonPanel = new JPanel(new GridLayout(4, 4, 36, 66));

  buttonPanel.setBorder(BorderFactory.createEmptyBorder(6, 16, 16, 16));

  getContentPane().add(buttonPanel, BorderLayout.CENTER);

  buttonPanel.add(new JButton(incrAction));

  buttonPanel.add(new JButton(gotoAction));

  buttonPanel.add(new JButton(decrAction));

  buttonPanel.add(new JButton(setGoToAction));

  JMenuBar menuBar = new JMenuBar();

  JMenu menu = new JMenu("Number");

  menu.add(new JMenuItem(incrAction));

  menu.add(new JMenuItem(decrAction));

  menu.addSeparator();

  menu.add(new JMenuItem(gotoAction));

  menu.add(new JMenuItem(setGoToAction));

  menuBar.add(menu);

  setJMenuBar(menuBar);
    }

    public void setChannel(int chan) {

  curr_chanel = chan;

  channelLabel.setText("Number is: " + curr_chanel);

  decrAction.setEnabled(curr_chanel > Min_Channel);

  incrAction.setEnabled(curr_chanel < MAX_CHANNEL);

  gotoAction.setEnabled(curr_chanel != gotoChannel);

  setGoToAction.setEnabled(curr_chanel != gotoChannel);
    }

    public static void main(String argv[]) {

  JFrame Jframe = new Main();

  Jframe.setSize(800, 600);

  Jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  Jframe.setVisible(true);
    }
}

 
This was an example on how to use the Action class in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button