Home » Desktop Java » awt » event » Change Event behavior dynamically

About Byron Kiourtzoglou

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.

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 {

  @Override

  public void actionPerformed(ActionEvent event) {

textArea.append("Button was pressedn");

  }
    }

    class EventCounter implements ActionListener {

  private int index;

  public EventCounter(int indx) {

index = indx;

  }

  @Override

  public void actionPerformed(ActionEvent event) {

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

  }
    }

    class B1 implements ActionListener {

  @Override

  public void actionPerformed(ActionEvent event) {

textArea.append("A pressedn");

ActionListener actListener = new EventCounter(i++);

list.add(actListener);

button2.addActionListener(actListener);

  }
    }

    class B2 implements ActionListener {

  @Override

  public void actionPerformed(ActionEvent event) {

textArea.append("B pressedn");

int end = list.size() - 1;

if (end >= 0) {

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

    list.remove(end);

}

  }
    }

    @Override
    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();

  jPanel.add(button1);

  jPanel.add(button2);

  container.add(BorderLayout.NORTH, jPanel);

  container.add(new JScrollPane(textArea));
    }

    public static void main(String[] args) {

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

@Override

public void run() {

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

}

  });
    }

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

  JFrame jFrame = new JFrame();

  jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  jFrame.getContentPane().add(app);

  jFrame.setSize(w, h);

  app.init();

  app.start();

  jFrame.setVisible(true);

    }
}

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

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of