ERLEDIGT
JA
JA
ANTWORTEN
7
7
ZUGRIFFE
4525
4525
EMPFEHLEN
-
Moin Moin.
Ich möchte für bestimmte Textfelder festlegen welche Tasten angenommen werden sollen. Über einen KeyListener wäre die Sache ja sehr unschön (wenn die gedrückte Taste nicht erlaubt -> entferne den Buchstaben wieder). Ich hatte erst gedacht ich könnte es über eine InputMap machen, aber da werden ja auch irgendwelche ActionListener ausgeführt (oder hab ich da was falsch verstanden).
Er soll z.B nur Zahlen annehmen.
Ich hoffe ihr könnt mir helfen.
Thx
-
Hallole,
Zitat von Vatar
Das Problem ist ein sehr typisches und verlangt eine einigermaßen allgemeine Lösung.
In der Vergangenheit hatte ich es auch mal mit Keylistener gelöst und dabei mit einer Methode consume() die entsprechende nichtgewollte eingabe unterbunden. Das funktionierte soweit ganz nett im Windows aber in Unix funktionierte es irgendwie nicht. Der bessere Weg ist mit den "Dokumenten" der Text-Komponenten einen Lösungsatz zu wagen. Das fürhre mich zu einem ganz guten Ergebnissen....
(s.o. folgende Mails)
-
hier die Definitionen von verwendeten Konstanten...
Hier das Document von dem ich schon in obiger Nachricht sprach...Code :1 2 3 4 5 6 7 8 9 10 11 12
package de.cmk.gui; public interface LimitedTextInputModes { public static final int _ANYTHING = 0; public static final int _SPECIALS_ONLY = 1; public static final int _LETTERS_ONLY = 2; public static final int _DIGITS_ONLY = 3; public static final int _LETTERS_SPECIALS_ONLY = 4; public static final int _DIGITS_SPECIALS_ONLY = 5; public static final int _LETTERS_DIGITS_SPECIALS_ONLY = 6; }
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
package de.cmk.gui; import javax.swing.text.*; import java.awt.*; import java.util.*; public class LimitedInputDocument extends PlainDocument implements LimitedTextInputModes { private int charLimitMax = 0; // maximale Anzahl erlaubter Zeichen private int limitMode = _ANYTHING; // private String specials = new String(); private String forbiddenSpecials = new String(); private ArrayList editableAreas = new ArrayList(); public LimitedInputDocument() { } public void setMode(int mode) { limitMode = mode; if (limitMode < _ANYTHING || mode > _LETTERS_DIGITS_SPECIALS_ONLY) { throw new IllegalArgumentException("unbekannter Modus!"); } } public void setMaxChar(int maxChar) { charLimitMax = maxChar; } public void setSpecials(String allowedSpecials) { specials = allowedSpecials; } public void setForbiddenSpecials(String forbiddenSpecials) { this.forbiddenSpecials = forbiddenSpecials; } /** * fügt eine editierbare Region in dem Textdokument ein * wird gar keine jemals angegeben, ist das gesammt Dokument prinzipiell editierbar * @param start int * @param end int * @throws BadLocationException * @see boolean isInEditableArea(int offset) */ public void addEditableArea(int start, int end) throws BadLocationException { editableAreas.add(new Area(createPosition(start),createPosition(end))); } public void removeAllEditableAreas() {// was mit den Positionen im Document passiert ist ungewiss // müssten eigetnlich ungenutzt stehen bleiben editableAreas.clear(); } public void removeEditableArea(int start, int end) { for (int i=editableAreas.size()-1; i>=0; i--) { Area a = (Area)editableAreas.get(i); if (a.start == start && a.end == end) { editableAreas.remove(i); } } } public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str == null) { str = ""; } char[] source = str.toCharArray(); char[] result = new char[source.length]; int j = 0; for (int i = 0; i < result.length; i++) { boolean charToBeIgnored = false; switch (limitMode) { case _ANYTHING: break; case _SPECIALS_ONLY: charToBeIgnored=!isSpecialChar(source[i]); break; case _LETTERS_ONLY: charToBeIgnored=!Character.isLetter(source[i]); break; case _DIGITS_ONLY: charToBeIgnored=!Character.isDigit(source[i]); break; case _LETTERS_SPECIALS_ONLY: charToBeIgnored=!Character.isLetter(source[i]) && !isSpecialChar(source[i]); break; case _DIGITS_SPECIALS_ONLY: charToBeIgnored=!Character.isDigit(source[i]) && !isSpecialChar(source[i]); break; case _LETTERS_DIGITS_SPECIALS_ONLY: charToBeIgnored=!Character.isLetterOrDigit(source[i]) && !isSpecialChar(source[i]); break; default: System.out.println("falscher Modus im Textfeld "+this.toString()+" durchgeflutscht"); }// end of switch(limitMode) if (!charToBeIgnored && !isForbiddenChar(source[i]) && fitsInLength(j) && isInEditableArea(offs)) { result[j++] = source[i]; } else { Toolkit.getDefaultToolkit().beep(); } } super.insertString(offs, new String(result, 0, j), a); }// end of insertString(int, String, AttributeSet) public void remove(int offset, int len) throws BadLocationException { if (isFullEditable(offset, len)) { super.remove(offset,len); } else { Toolkit.getDefaultToolkit().beep(); } } protected boolean isSpecialChar(char keyChar) { boolean charOk = false; char [] sC = specials.toCharArray(); for(int i=0; !charOk && i<specials.length();i++) { if (keyChar==sC[i]) { charOk=true; } }// end of for i return charOk||specials.length()==0 ? true:false; }// end of isSpecialChar(char) protected boolean isInEditableArea(int offset) { boolean isInEditableArea = false; if (editableAreas.size()>0) { for (int i = 0; i<editableAreas.size() && !isInEditableArea; i++) { Area a = (Area)editableAreas.get(i); isInEditableArea = a.laysInArea(offset); } } else { isInEditableArea = true; } return isInEditableArea; } /** * Analysiert ob der Bereich editierbar ist, d.h. wenn * editierbare Areale angegeben wurden wird geschaut, ob * der durch die Parameter gegebene Bereich vollständig * in diesen Arealen liegt. Sind keine editierbaren Areale angegeben * wird davon ausgegangen, dass alles editierbar ist. * @param offset Offset im Document als Start des Bereiches * @param len Länge des Bereiches * @return true : wenn der angegebene Bereich vollständig in ein oder mehere * angegebene editierbare Bereich liegt, oder wenn kein * Bereich angegeben wurde. * false : wenn nicht der gesammte angegebene Bereich in editierbaren * Bereichen liegt, falls editierbare Bereich angegeben sind * @see addEditableArea (int, int) * @see Area */ protected boolean isFullEditable(int offset, int len) { boolean isFullEditable = true; if (editableAreas.size()>0) { boolean[] editables = new boolean[len]; for (int i = 0; i<editables.length; i++) { editables[i] = false; } boolean isFullIncluded = false; for (int i = 0; i<editableAreas.size() && !isFullIncluded; i++) { Area a = (Area)editableAreas.get(i); isFullIncluded = a.analyseInterception(offset, editables); } for (int i = 0; i<editables.length && isFullEditable; i++) { isFullEditable = editables[i]; } } return isFullEditable; } protected boolean fitsInLength(int addedCharCount) { return (charLimitMax<=0 || charLimitMax > addedCharCount+getLength()); } protected boolean isForbiddenChar(char keyChar) { boolean charOk = false; char [] sC = forbiddenSpecials.toCharArray(); for(int i=0; !charOk && i<forbiddenSpecials.length();i++) { if (keyChar==sC[i]) { charOk=true; } }// end of for i return charOk; }// end of isSpecialChar(char) private class Area { private Position startPos; private Position endPos; private int start; private int end; public Area(Position start, Position end) { this.startPos = start; this.endPos = end; this.start = start.getOffset(); this.end = end.getOffset(); } public int getStart() { return startPos.getOffset(); } public int getEnd() { return endPos.getOffset(); } /** * * @param offset * @param editables (boolean []) Elemente werden auf true gesetzt, wenn * diese Stelle in der Area in Bezug auf offset * eingeschlossen ist. * @return true wenn gegebene Parameter vollständig in der Area liegen * sonst false */ public boolean analyseInterception(int offset, boolean [] editables) { int end = getEnd(); int start = getStart(); int len =0; if (start<offset) { for (int i=offset, x=0; i<end && x<editables.length; i++,x++) { editables[x]=true; len++; } } else if (start>offset) { for (int i=start, x = start-offset; i<end && x<editables.length; i++,x++) { editables[x]=true; len++; } } else // offsets sind identisch { for (int i = 0; i<end && i<editables.length; i++) { editables[i]=true; len++; } } return editables.length == len; }// end of intercept(int, boolean[]) public boolean laysInArea(int p) { return startPos.getOffset()<p && p<=endPos.getOffset(); } public boolean equals(Object o) { boolean rc = false; if (o!=null) { if (o==this) { rc = true; } else if (o instanceof Area) { if (((Area)o).end==this.end && ((Area)o).start==this.start) { rc = true; } } } return rc; } }// end of class Area }// end of class LimitedInputDocument
die Verwendung des Documents kommt in folgender Mail....
-
Hier das ganze für eine "Textfläche"...
Und nun noch für Deinen eigentlichen Belang , einem Textfeld...Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
package de.cmk.gui; import javax.swing.*; import javax.swing.text.*; public class LimitedTextArea extends JTextArea implements LimitedTextInputModes { public LimitedTextArea() { super(); } public LimitedTextArea(int rows, int columns) { super(rows, columns); } public LimitedTextArea(int rows, int columns, int maxChar) { this(rows, columns); setMaxChar(maxChar); } public LimitedTextArea(int rows, int columns, int maxChar, int mode) { this(rows, columns, maxChar); setMode(mode); } public LimitedTextArea(int rows, int columns, int maxChar, int mode, String allowedSpecials) { this(rows, columns, maxChar, mode); setSpecials(allowedSpecials); } protected Document createDefaultModel() { return new LimitedInputDocument(); } public void setForbiddenSpecials(String forbiddenSpecials) { Document doc = getDocument(); if (doc instanceof LimitedInputDocument) { ((LimitedInputDocument)doc).setForbiddenSpecials(forbiddenSpecials); } } public void setMode(int mode) { Document doc = getDocument(); if (doc instanceof LimitedInputDocument) { ((LimitedInputDocument)doc).setMode(mode); } } public void setMaxChar(int maxChar) { Document doc = getDocument(); if (doc instanceof LimitedInputDocument) { ((LimitedInputDocument)doc).setMaxChar(maxChar); } } public void setSpecials(String allowedSpecials) { Document doc = getDocument(); if (doc instanceof LimitedInputDocument) { ((LimitedInputDocument)doc).setSpecials(allowedSpecials); } } } // end of class LimitedTextArea
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
package de.cmk.gui; import javax.swing.*; import javax.swing.text.*; public class LimitedTextField extends JTextField implements LimitedTextInputModes { public LimitedTextField() { super(); } public LimitedTextField(int columns) { super (columns); } protected Document createDefaultModel() { return new LimitedInputDocument(); } public LimitedTextField(int columns, int maxChar) { this(columns); setMaxChar(maxChar); } public LimitedTextField(int columns, int maxChar, int mode) { this(columns, maxChar); setMode(mode); } public LimitedTextField(int columns, int maxChar, int mode, String allowedSpecials) { this(columns, maxChar, mode); setSpecials(allowedSpecials); } public void setForbiddenSpecials(String forbiddenSpecials) { Document doc = getDocument(); if (doc instanceof LimitedInputDocument) { ((LimitedInputDocument)doc).setForbiddenSpecials(forbiddenSpecials); } } public void setMode(int mode) { Document doc = getDocument(); if (doc instanceof LimitedInputDocument) { ((LimitedInputDocument)doc).setMode(mode); } } public void setMaxChar(int maxChar) { Document doc = getDocument(); if (doc instanceof LimitedInputDocument) { ((LimitedInputDocument)doc).setMaxChar(maxChar); } } public void setSpecials(String allowedSpecials) { Document doc = getDocument(); if (doc instanceof LimitedInputDocument) { ((LimitedInputDocument)doc).setSpecials(allowedSpecials); } } }// end of class LimitedTextField
Viel Spaß damit
Takidoso
-
vielen vielen dank.
Ich werd mich mal durchackern und meld mich wenn ich es geschafft hab.
edit:
ABSOLUT GENIAL. Nochmals vielen Dank. Funktioniert einwandfrei (jetzt muss ich nur noch kapieren. bisher wars nur copy&paste
)
Geändert von Vatar (18.04.05 um 13:26 Uhr)
-
Öh, ich kann mich des Eindrucks nicht erwehren, dass es *wesentlich* einfacher ist, beim JTextField ein setDocument() aufzurufen und ein eigenes, von PlainDocument abgeleitetes Document zu verwenden. Dort
Code :1 2 3 4
void insertString(int offset, String str, AttributeSet a) throws BadLocationException
überschreiben und gut ist.
-
19.04.05 13:03 #7
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo!
Da muss ich Snape mal recht geben...:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/** * */ package de.tutorials.training; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; /** * @author Darimont * */ public class LimitedJTextfieldExample extends JFrame { JTextField jtf; public LimitedJTextfieldExample() { super("LimitedJTextfieldExample"); setDefaultCloseOperation(EXIT_ON_CLOSE); jtf = new JTextField(20); jtf.setDocument(new PlainDocument() { public void insertString(int offset, String str, AttributeSet a) throws BadLocationException { // Eingaben von a und b sind nicht erlaubt... if (str.matches(".*[a|b].*")) { return; } super.insertString(offset, str, a); } }); add(jtf); pack(); setVisible(true); } /** * @param args */ public static void main(String[] args) { new LimitedJTextfieldExample(); } }
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
Hi,
ja diese Art ist sicher kürzer. allerdings muss ich zu meiner Lösung hinzufügen, dass sie entstand bevor es Regular Expressions in Java gab und ich eine möglichst kurze allgemeine Form haben wollte die einigermaßen kurz und sprechend im Code ist und dem Anwendungsentwickler nicht mit notwendigem Wissen über Dokument-Objekten für Textkomponenten zu belasten. Später kam dann noch der Wunsch auf in z.B. Textareas bestimmte Bereiche readonly machen zu können.
Aber Deine Lösung Tom gefällt mir auch ganz prima
Takidoso
Ähnliche Themen
-
Nur bestimmte Links zulassen, bzw Validierung
Von typhoch2 im Forum Javascript & AjaxAntworten: 1Letzter Beitrag: 09.03.10, 19:00 -
nur bestimmte Zeichen zulassen...
Von ruNN0r im Forum PHPAntworten: 2Letzter Beitrag: 23.05.08, 21:21 -
[Hibernate] nur bestimmte Werte zulassen
Von Vatar im Forum JavaAntworten: 2Letzter Beitrag: 28.11.06, 18:26 -
bestimmte URL zulassen
Von makitaman im Forum PHPAntworten: 16Letzter Beitrag: 16.05.05, 10:53 -
Textfeld nur Zahlen zulassen
Von kevkev im Forum Javascript & AjaxAntworten: 12Letzter Beitrag: 18.12.04, 15:26





Zitieren

Login





