Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Toolkit;

import java.awt.event.KeyEvent;

import javax.swing.JTextField;

import javax.swing.KeyStroke;

import javax.swing.text.DefaultEditorKit;
import javax.swing.text.JTextComponent;
import javax.swing.text.Keymap;

public class Main {
    /**
     * initialize keystroke bindings
     */
    public final static void InitializeKeyStrokeBindings() {
        String selectAllAction = DefaultEditorKit.selectAllAction;
        String cutAction = DefaultEditorKit.cutAction;
        String copyAction = DefaultEditorKit.copyAction;
        String pasteAction = DefaultEditorKit.pasteAction;

        int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

        JTextComponent.KeyBinding ctrlA = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_A, mask),
                selectAllAction);
        JTextComponent.KeyBinding ctrlX = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_X, mask),
                cutAction);
        JTextComponent.KeyBinding ctrlC = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_C, mask),
                copyAction);
        JTextComponent.KeyBinding ctrlV = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_V, mask),
                pasteAction);

        JTextComponent.KeyBinding[] extraBindings = new JTextComponent.KeyBinding[] { ctrlA, ctrlX, ctrlC, ctrlV };

        Keymap defaultKeyMap = JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP);

        JTextComponent dummy = new JTextField();
        JTextComponent.loadKeymap(defaultKeyMap, extraBindings, dummy.getActions());
    }
}