Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JTextArea;
import javax.swing.text.JTextComponent;
import javax.swing.text.Keymap;

public class Main {
    public static void main(String[] argv) throws Exception {
        JTextArea component = new JTextArea();
        Action defAction = findDefaultAction(component);
        component.getKeymap().setDefaultAction(new MyDefaultAction(defAction));
    }

    public static Action findDefaultAction(JTextComponent c) {
        Keymap kmap = c.getKeymap();
        if (kmap.getDefaultAction() != null) {
            return kmap.getDefaultAction();
        }

        kmap = kmap.getResolveParent();
        while (kmap != null) {
            if (kmap.getDefaultAction() != null) {
                return kmap.getDefaultAction();
            }
            kmap = kmap.getResolveParent();
        }
        return null;
    }
}

class MyDefaultAction extends AbstractAction {
    Action defAction;

    public MyDefaultAction(Action a) {
        super("My Default Action");
        defAction = a;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() != null) {
            String command = e.getActionCommand();
            if (command != null) {
                command = command.toUpperCase();
            }
            e = new ActionEvent(e.getSource(), e.getID(), command, e.getModifiers());
        }

        if (defAction != null) {
            defAction.actionPerformed(e);
        }
    }
}