Question of shortcuts in Swing [Textarea]

xiaoxiao2021-03-06  141

problem:

In Swing JTextarea, we knocked back the car key is a wrap. Now I want to replace Ctrl Enter, I don't know how this event should be handled? ? I can already judge that Ctrl Enter is pressed simultaneously, but I don't know how to reach only the effect of ENTER. For the original Enter key, the current function is to allow it to move to the next control.

solve:

Write a class inherited in JTextarea and achieve several ways to instance. Or in our own class, add the following method.

Import hanksoftbuhin.htextarea;

Import java.awt.event.inputevent; import java.awt.event.keyevent; import java.util.hashtable;

Import javax.swing.action; import javax.swing.keystroke; import javax.swing.text.default; import javax.swing.text.jtextComponent; import javax.swing.text.KeyMap;

/ * * Created on 2004-8-13 * * to change the Template for this generated file go to * window> preferences> java> code generation> code and comments * /

/ ** * @author Administrator * * To change the template for this generated type comment go to * Window> Preferences> Java> Code Generation> Code and Comments * / public class TestArea extends HTextArea {private Hashtable actionTable = new Hashtable (); Public Testarea () {Super (); loadActionTable (); setKeyMap ();}

/ ** * In KeyMap, add the processing of Ctrl ENTER simultaneously. * @Return * / private keyMap createKeyMap () {keymap map = jtextcomponent.addkeymap ("applet keymap", getKeyMap ());

Keystroke ReturnkeyStroke = keyStroke.getKeystroke (keyevent.vk_enter, infutevent.ctrl_mask); action returnction = getAction (defaultitorkit.insertbreakaction);

Map.addactionForKeystroke (RETURNKEYSTROKE, RETURNACTION); RETURN MAP;} / ** * Get a collection of original action * * / private void loadActionTable () {action [] ActionS = getActions ();

For (int i = 0; i

==============================

As for the function of shielding the original Enter, it is now replaced with another method, and does not find the real remove to drop the method.

Expand the CreateKeyMap () method.

Private keyMap createKeyMap () {keymap map = jtextcomponent.addkeymap ("applet keymap", getKeyMap ());

Keystroke ReturnkeyStroke = keyStroke.getKeystroke (keyevent.vk_enter, infutevent.ctrl_mask); action returnction = getAction (defaultitorkit.insertbreakaction);

Map.addActionForKeystroke (ReturnkeyStroke, returnction);

KeyStroke returnKeyStroke2 = KeyStroke.getKeyStroke (KeyEvent.VK_ENTER, 0); Action returnAction2 = getAction (DefaultEditorKit.endWordAction); map.addActionForKeyStroke (returnKeyStroke2, returnAction2); return map;}

This is immediately removed from the original wrap, but it is not fundamentally resolved.

转载请注明原文地址:https://www.9cbs.com/read-127597.html

New Post(0)