Source Code : Overriding the Default Action of a JTextComponent
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Overriding the Default Action of a JTextComponent
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);
}
}
}
Thank with us