KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > KeyboardBindingsExample


1 /*
2  * $Id: KeyboardBindingsExample.java,v 1.5 2004/12/01 07:54:05 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package wingset;
15
16 import org.wings.SComponent;
17 import org.wings.SForm;
18 import org.wings.SLabel;
19 import org.wings.STextField;
20
21 import javax.swing.*;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24
25 /**
26  * @author <a HREF="mailto:hengels@mercatis.de">Holger Engels</a>
27  * @version $Revision: 1.5 $
28  */

29 public class KeyboardBindingsExample extends WingSetPane {
30     private SLabel label = new SLabel();
31     private STextField textField = new STextField();
32     private SForm form = new SForm();
33
34     public SComponent createExample() {
35         InputMap inputMap = new InputMap();
36         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0, false), "f1");
37         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0, false), "f2");
38         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0, false), "f3");
39         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0, false), "f4");
40         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0, false), "f5");
41         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0, false), "f6");
42         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0, false), "f7");
43         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0, false), "f8");
44         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0, false), "f9");
45         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0, false), "f10");
46
47         InputMap formInputMap = new InputMap();
48         formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, KeyEvent.SHIFT_DOWN_MASK, false), "shift f1");
49         formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, KeyEvent.SHIFT_DOWN_MASK, false), "shift f2");
50         formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, KeyEvent.SHIFT_DOWN_MASK, false), "shift f3");
51         formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, KeyEvent.SHIFT_DOWN_MASK, false), "shift f4");
52         formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, KeyEvent.SHIFT_DOWN_MASK, false), "shift f5");
53         formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, KeyEvent.SHIFT_DOWN_MASK, false), "shift f6");
54         formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F7, KeyEvent.SHIFT_DOWN_MASK, false), "shift f7");
55         formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F8, KeyEvent.SHIFT_DOWN_MASK, false), "shift f8");
56         formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F9, KeyEvent.SHIFT_DOWN_MASK, false), "shift f9");
57         formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, KeyEvent.SHIFT_DOWN_MASK, false), "shift f10");
58
59
60         Action action = new AbstractAction() {
61             public void actionPerformed(ActionEvent JavaDoc e) {
62                 label.setText(e.getActionCommand());
63             }
64         };
65
66         ActionMap actionMap = new ActionMap();
67         actionMap.put("f1", action);
68         actionMap.put("f2", action);
69         actionMap.put("f3", action);
70         actionMap.put("f4", action);
71         actionMap.put("f5", action);
72         actionMap.put("f6", action);
73         actionMap.put("f7", action);
74         actionMap.put("f8", action);
75         actionMap.put("f9", action);
76         actionMap.put("f10", action);
77         actionMap.put("shift f1", action);
78         actionMap.put("shift f2", action);
79         actionMap.put("shift f3", action);
80         actionMap.put("shift f4", action);
81         actionMap.put("shift f5", action);
82         actionMap.put("shift f6", action);
83         actionMap.put("shift f7", action);
84         actionMap.put("shift f8", action);
85         actionMap.put("shift f9", action);
86         actionMap.put("shift f10", action);
87
88         textField.setInputMap(inputMap);
89         textField.setActionMap(actionMap);
90
91         form.setInputMap(formInputMap);
92         form.setActionMap(actionMap);
93
94         form.add(new SLabel("<html>f1 through f10 are captured by the STextField<br/>" +
95                 "shift f1 through shift f10 are bubbling up to the containing SForm"));
96         form.add(textField);
97         form.add(label);
98
99         return form;
100     }
101 }
102
Popular Tags