Java Swing Tutorial - Java ActionEvent(Object source, int id, String command, long when, int modifiers) Constructor


ActionEvent(Object source, int id, String command, long when, int modifiers) constructor from ActionEvent has the following syntax.

public ActionEvent(Object source,   int id,    String command,   long when,   int modifiers)


In the following code shows how to use ActionEvent.ActionEvent(Object source, int id, String command, long when, int modifiers) constructor.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.EventListener;
//w w w  .  ja va2  s  .  c  om
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.EventListenerList;

public class Main {

  public static void main(String args[]) {
    JFrame frame = new JFrame("Type tab to and then type");
    KeyTextComponent keyTextComponent = new KeyTextComponent();
    final JTextField textField = new JTextField();

    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        String keyText = actionEvent.getActionCommand();

    frame.add(keyTextComponent, BorderLayout.CENTER);
    frame.add(textField, BorderLayout.SOUTH);
    frame.setSize(300, 200);

class KeyTextComponent extends JComponent {
  private EventListenerList actionListenerList = new EventListenerList();

  public KeyTextComponent() {
    KeyListener internalKeyListener = new KeyAdapter() {
      public void keyPressed(KeyEvent keyEvent) {
        if (actionListenerList != null) {
          int keyCode = keyEvent.getKeyCode();
          String keyText = KeyEvent.getKeyText(keyCode);
          ActionEvent actionEvent = new ActionEvent(this,
              ActionEvent.ACTION_FIRST, keyText,System.currentTimeMillis()-10,ActionEvent.ALT_MASK);

    MouseListener internalMouseListener = new MouseAdapter() {
      public void mousePressed(MouseEvent mouseEvent) {


  public void addActionListener(ActionListener actionListener) {
    actionListenerList.add(ActionListener.class, actionListener);

  public void removeActionListener(ActionListener actionListener) {
    actionListenerList.remove(ActionListener.class, actionListener);

  protected void fireActionPerformed(ActionEvent actionEvent) {
    EventListener listenerList[] = actionListenerList
    for (int i = 0, n = listenerList.length; i < n; i++) {
      ((ActionListener) listenerList[i]).actionPerformed(actionEvent);

  public boolean isFocusable() {
    return true;