Change Event behavior dynamically

In this example we are going to see how to change the behavior of an event dynamically in your Java program.

It is very simple to add this kind of functionality in your application. All you have to do is:

  • Create some classes that implement the ActionListener interface.
  • Bundle these ActionListener with a specific button or component using addActionListener method.
  • According to your need you can dynamically remove an ActionListener using removeActionListener and thus change the behavior of a specific event.

Let’s take a closer look at 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.util.ArrayList;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class DynamicEvents extends JApplet {

    private java.util.List list = new ArrayList();
    private int i = 0;

    private JButton button1 = new JButton("A");
    private JButton button2 = new JButton("B");
    private JTextArea textArea = new JTextArea();

    class B implements ActionListener {


  public void actionPerformed(ActionEvent event) {

textArea.append("Button was pressedn");


    class EventCounter implements ActionListener {

  private int index;

  public EventCounter(int indx) {

index = indx;



  public void actionPerformed(ActionEvent event) {

textArea.append("Counted Listener " + index + "n");


    class B1 implements ActionListener {


  public void actionPerformed(ActionEvent event) {

textArea.append("A pressedn");

ActionListener actListener = new EventCounter(i++);




    class B2 implements ActionListener {


  public void actionPerformed(ActionEvent event) {

textArea.append("B pressedn");

int end = list.size() - 1;

if (end >= 0) {

    button2.removeActionListener((ActionListener) list.get(end));




    public void init() {

  Container container = getContentPane();

  button1.addActionListener(new B());

  button1.addActionListener(new B1());

  button2.addActionListener(new B());

  button2.addActionListener(new B2());

  JPanel jPanel = new JPanel();



  container.add(BorderLayout.NORTH, jPanel);

  container.add(new JScrollPane(textArea));

    public static void main(String[] args) {

   javax.swing.SwingUtilities.invokeLater(new Runnable() {


public void run() {

    showUI(new DynamicEvents(),800,600);



    public static void showUI(JApplet app, int w, int h) {

  JFrame jFrame = new JFrame();



  jFrame.setSize(w, h);





This was an example on how to Change Event behavior dynamically.

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.
Notify of

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

Inline Feedbacks
View all comments
Back to top button