KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > swing > EntryTextArea


1 package net.suberic.util.swing;
2
3 import javax.swing.*;
4 import javax.swing.text.*;
5 import java.awt.event.KeyEvent JavaDoc;
6 import java.awt.AWTKeyStroke JavaDoc;
7 import java.awt.KeyboardFocusManager JavaDoc;
8 import java.util.*;
9
10 /**
11  * This is a subclass of JTextArea which allows for tab field navigation.
12  */

13 public class EntryTextArea extends JTextArea {
14
15   boolean keysUpdated = false;
16   /**
17    * Constructor; calls super.
18    */

19   public EntryTextArea() {
20     super();
21   }
22   
23   /**
24    * Constructor; calls super.
25    */

26   public EntryTextArea(Document doc) {
27     super(doc);
28     updateFocusTraversalKeys();
29   }
30
31   /**
32    * Constructor; calls super.
33    */

34   public EntryTextArea(Document doc, String JavaDoc text, int rows, int columns) {
35     super(doc, text, rows, columns);
36     updateFocusTraversalKeys();
37   }
38   
39   /**
40    * Constructor; calls super.
41    */

42   public EntryTextArea(int rows, int columns) {
43     super(rows, columns);
44     updateFocusTraversalKeys();
45   }
46   
47   /**
48    * Constructor; calls super.
49    */

50   public EntryTextArea(String JavaDoc text) {
51     super(text);
52     updateFocusTraversalKeys();
53   }
54   
55   /**
56    * Constructor; calls super.
57    */

58   public EntryTextArea(String JavaDoc text, int rows, int columns) {
59     super(text, rows, columns);
60     updateFocusTraversalKeys();
61   }
62
63   /**
64    * updates the focus traversal keys to include tab and shift-tab.
65    */

66   protected void updateFocusTraversalKeys() {
67     if (! keysUpdated) {
68       keysUpdated=true;
69       Set forward = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
70       
71       Set newForward = new HashSet(forward);
72       newForward.add(AWTKeyStroke.getAWTKeyStroke("pressed TAB"));
73
74       setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
75                 newForward);
76
77       Set backward = getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
78       
79       Set newBackward = new HashSet(backward);
80       newBackward.add(AWTKeyStroke.getAWTKeyStroke("shift pressed TAB"));
81
82       setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
83                 newBackward);
84     }
85   }
86
87 }
88
Popular Tags