ERLEDIGT
JA
JA
ANTWORTEN
1
1
ZUGRIFFE
737
737
EMPFEHLEN
-
Hallo!
Ich habe mir eine eigene Klasse geschrieben, die ich von PlainDocument ableite und somit die insertString()-Methode überschreibe. Mit diesem Vorgehen ermögliche ich mir, die Eingabe auf bestimmte Kriterien zu überprüfen.
Zudem habe ich das Observer-Pattern integriert, damit bei falscher Eingabe das Feld rot markiert wird.
Das funktioniert soweit auch, nur wird derzeit das Feld solange rot angezeigt, bis alle Kriterien erfüllt sind. Ich würde es aber gerne so programmieren, das erst nach kompletter Eingabe überprüft wird, ob alle Kriterien erfüllt sind.
Ich schätze mal, dass ich dann irgendwie mmit dem Focus arbeiten muss, aber ich ahbe keine richtige Idee, wie ich das realisieren kann.
Ich hoffe, Ihr könnt mir helfen.
Hier der Code der Document-Klasse:
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
package validation; import java.awt.Toolkit; import java.util.Observable; import java.util.Observer; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; /** * This class controls the entry (=MODEL). * Only numbers are allowed. * This class is coded so that it is possible(not stringent) * to integrate an observer in the VIEW. * @author Stepanski * */ @SuppressWarnings("serial") public class EmailDocument extends PlainDocument { // create an inner class for knowing the // setChanged()-method class DocumentObservable extends Observable { public void setChanged(){ super.setChanged(); } } // integrate an observable private DocumentObservable observable = new DocumentObservable(); /** * This constructor is for a solution without an * observer in the VIEW */ public EmailDocument(){ } /** * This constructor is for a solution with observer * in the VIEW (TextComponent) */ public EmailDocument(Observer o){ this.observable.addObserver(o); } /** * This method checks the entry */ public void checkContents(){ // acting on the assumption of an invalid entry Boolean isValid = this.getLength()==0?Boolean.TRUE:Boolean.FALSE; /* IS: * if (this.getLength()==0) * this.isValid = Boolean.TRUE; * else * this.isValid = Boolean.FALSE;*/ // checking whether the entry integrates '@' and '.XX' String s; try { s = this.getText(0, this.getLength()); if (!s.contains(" ") && s.contains("@") && s.contains(".org") || s.contains(".de") || s.contains(".com")) isValid = Boolean.TRUE; else isValid = Boolean.FALSE; } catch (BadLocationException e) { Toolkit.getDefaultToolkit().beep(); } this.observable.setChanged(); this.observable.notifyObservers(isValid); } @Override public void insertString(int offset, String str, AttributeSet as) throws BadLocationException { // Isn't there an observer? if (this.observable.countObservers() == 0){ try{ this.checkContents(); super.insertString(offset, str, as); }catch (Exception e){ Toolkit.getDefaultToolkit().beep(); } } else{ super.insertString(offset, str, as); this.checkContents(); } } @Override public void remove(int offset, int length) throws BadLocationException { super.remove(offset, length); this.checkContents(); } }
Und hier die relevanten Quell-Code-Ausschnitte aus der Klasse, die die GUI erstellt:
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
private Observer textFieldEmailObserver = new Observer(){ @Override public void update(Observable o, Object arg) { Boolean isValid = (Boolean)arg; if (Client.tfempf.hasFocus()){ if (isValid.booleanValue()) { Client.tfempf.setBackground(Color.WHITE); }else{ Client.tfempf.setBackground(Color.RED); } }else if (Client.tfcc.hasFocus()){ if (isValid.booleanValue()) { Client.tfcc.setBackground(Color.WHITE); }else{ Client.tfcc.setBackground(Color.RED); } }else if (Client.tfbcc.hasFocus()){ if (isValid.booleanValue()) { Client.tfbcc.setBackground(Color.WHITE); }else{ Client.tfbcc.setBackground(Color.RED); } } } }; ... /*Konstruktor*/ /** * Diese Methode stellt den Konstruktor dar. * Sie erzeugt eine Instanz der Klasse Client, wodurch das Hauptfenster * geoeffnet wird. */ public Client(){ super("E-Mail Client"); this.createGUI(); this.setDocuments(); ... ... /** * This method links this class with a MODEL */ private void setDocuments () { Client.getTfempf().setDocument(new EmailDocument( this.textFieldEmailObserver)); Client.getTfcc().setDocument(new EmailDocument( this.textFieldEmailObserver)); Client.getTfbcc().setDocument(new EmailDocument( this.textFieldEmailObserver)); }
-
Hab es jetzt komplett anders gelöst:
InputVerifier
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
tfempf.setInputVerifier(new EMailVerifier()); ... private class EMailVerifier extends InputVerifier { @Override public boolean verify(JComponent input) { JTextField jTF = (JTextField) input; String s = jTF.getText(); // Bedingungen if ( "".equals(s) || !s.contains(" ") && s.contains("@") && s.contains(".org")|| s.contains(".de") || s.contains(".com")) return true; else return false; } @Override public boolean shouldYieldFocus(JComponent input){ if (!verify(input)){ input.setBackground(java.awt.Color.RED); return false; }else { input.setBackground(java.awt.Color.WHITE); return true; } } }
Alles andere fällt damit weg
Ähnliche Themen
-
StyledText - Neue Eingabe soll mit farbigem Hintergrund dargestellt werden
Von Prengepower im Forum JavaAntworten: 0Letzter Beitrag: 14.10.09, 17:30 -
[VC++ 6.0] Bei Eingabe von 'a' soll 'b' ausgegeben werden!
Von berwing im Forum C/C++Antworten: 3Letzter Beitrag: 14.12.07, 20:04 -
JTextField - Eingabe begrenzen
Von BladeS_MC im Forum Swing, Java2D/3D, SWT, JFaceAntworten: 1Letzter Beitrag: 20.05.07, 16:46 -
[C++] Eingabe in Konsole erst nach bestimmter Zeit zulassen
Von Freak im Forum C/C++Antworten: 6Letzter Beitrag: 09.04.07, 17:17 -
formularfeld-eingabe nach eingabe sofort verarbeiten?
Von Suchfunktion im Forum PHPAntworten: 6Letzter Beitrag: 14.04.05, 23:31





Zitieren
Login





