Article View: borland.public.jbuilder.java.swing
Article #12892Re: problem with inserting text in JTextField
From: Todd Shutts
Date: Fri, 30 Oct 1998 00:00
Date: Fri, 30 Oct 1998 00:00
76 lines
3246 bytes
3246 bytes
The information below is cobbed directly from the JavaDoc for
JTextField.
-Todd
Customized fields can easily be created by extending the model and
changing the default model provided. For example, the following piece of
code will create a field that holds only upper case characters. It will
work even if text is pasted into from the clipboard or it is altered via
programmatic changes.
public class UpperCaseField extends JTextField {
public UpperCaseField(int cols) {
super(cols);
}
protected Document createDefaultModel() {
return new UpperCaseDocument();
}
static class UpperCaseDocument extends PlainDocument {
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str == null) {
return;
}
char[] upper = str.toCharArray();
for (int i = 0; i < upper.length; i++) {
upper[i] = Character.toUpperCase(upper[i]);
}
super.insertString(offs, new String(upper), a);
}
}
}
Anne JANSSENS wrote:
> What I want to do is very simple : when the user enter a letter in
> the JTextFieldcomponent, I want to check the letter to put it in upper
> case if it's not! I have implemented a keyListener and the method
> keyPressed : import java.awt.event.*;
> import com.sun.java.swing.*; public class gestionEvent extends
> KeyAdapter
> { public void keyPressed(KeyEvent evt)
> { int code=evt.getKeyCode();
> char car=evt.getKeyChar();
> if (Character.isLowerCase(car))
> { evt.consume();
> char car2=Character.toUpperCase(car);
> JTextField un_champ=(JTextField)evt.getComponent();
> String chaine=un_champ.getText();
> chaine=chaine+car2;
> un_champ.setText(chaine);
> } } The letter is put in upper case and I ask setText to show
> the new string ! THE RESULT is that It shows in the JTextField
> component the letter in UpperCase + in LowerCase ! I have thought that
> it was the result of the Document associated with my JTextField, so I
> have created my owndocument associated with my JTextField and I wrote
> the methode insertUpdate : import com.sun.java.swing.text.*; public
> class un_document extends PlainDocument
> { public un_document()
> { super();
> } public void insertString(int off, String str,AttributeSet a)
> throws BadLocationException
> { char[] source=str.toCharArray();
> super.insertString(off,str,a);
> }} Two times the method insertString is called : with the letter in
> uppercase and lowercase !How can I catch the second call to
> insertString with the letter in lowercase ? Can someone help me in
> anyway ? Thanks a lot, Anne
Technical Details
Message-ID:
<363A1D8C.987F0B25@us.eeft.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<71c5a8$kf815@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,e3976472cf7f116c X-Google-Attributes: gid111d45,public From: Todd Shutts <todd@us.eeft.com> Subject: Re: problem with inserting text in JTextField Date: 1998/10/30 Message-ID: <363A1D8C.987F0B25@us.eeft.com> X-Deja-AN: 406779125 Content-Transfer-Encoding: 7bit References: <71c5a8$kf815@forums.borland.com> To: Anne JANSSENS <recherche@generix.fr> Content-Type: text/plain; charset=us-ascii Organization: Euronet Services, Inc. Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing