event

Java ActionListener example

In this tutorial we are going to see how ActionListener works in Java. This is one the most important components you have to work with when you’re developing a GUI Application. The ActionListener in able to monitor a number of important events that occur in GUI Apps.

In short, all you have to do to work with an ActionListener in Java is:

  • Create an ActionListener instance.
  • Override the methods that correspond to the events that you want to monitor about the components e.g, actionPerformed and customize as you wish the handling of the respective events. Now every time one of these events occurs, the corresponding method will be executed.
  • Use addActionListener to add the ActionListener to a specific component.

Let’s take a closer look a the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main {

    public static void main(String args[]) {

  JFrame jFrame = new JFrame();

  Container cPane = jFrame.getContentPane();

  ActionListener actListener = new ActionListener() {

@Override

public void actionPerformed(ActionEvent event) {

    System.out.println("Command: " + event.getActionCommand());

    System.out.println("Modifiers: ");

    int mods = event.getModifiers();

    System.out.println("   Alt  : "

+ cmodifiers(mods, ActionEvent.ALT_MASK));

    System.out.println("   Ctrl : "

+ cmodifiers(mods, ActionEvent.CTRL_MASK));

    System.out.println("   Meta : "

+ cmodifiers(mods, ActionEvent.META_MASK));

    System.out.println("   Shift: "

+ cmodifiers(mods, ActionEvent.SHIFT_MASK));

    Object obj = event.getSource();

    if (obj instanceof JComboBox) {

  JComboBox jb = (JComboBox) obj;

  System.out.println("Combo: " + jb.getSelectedItem());

    }

}

private boolean cmodifiers(int mods, int mask) {

    return ((mods & mask) == mask);

}

  };

  String array[] = {"Item 1", "Item 2", "Item 3"};

  JComboBox box = new JComboBox(array);

  box.setMaximumRowCount(10);

  box.setEditable(true);

  box.addActionListener(actListener);

  cPane.add(box, BorderLayout.NORTH);

  JButton jButton = new JButton("Button!");

  jButton.addActionListener(actListener);

  cPane.add(jButton, BorderLayout.CENTER);

  JPanel jPanel = new JPanel();

  JLabel label = new JLabel("");

  JTextField text = new JTextField("Type here", 22);

  text.addActionListener(actListener);

  label.setDisplayedMnemonic(KeyEvent.VK_1);

  label.setLabelFor(text);

  jPanel.add(label);

  jPanel.add(text);

  cPane.add(jPanel, BorderLayout.SOUTH);

  jFrame.pack();

  jFrame.setVisible(true);
    }
}

 
This was an example on how to work with ActionListener 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