StephenK
August 9th, 1999, 07:36 AM
In Sun's documentation, I found a nice class, based on JTextField, that will convert all typing and pasting
into UPPER CASE. The code appears below.
I have found this very useful.
However, I would also like to have a similar class, say UpperCaseTextArea, that
extends JTextArea and converts typing and pasting into UPPER CASE.
Perhaps one of you guys can provide that. It's likely that many
other people would find that useful.
Thanks,
Steve
stephen@basit.com
------------------------------------------------------------------------------------------------
import javax.swing.JTextField;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
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);
}
}
}
//---- end--------
into UPPER CASE. The code appears below.
I have found this very useful.
However, I would also like to have a similar class, say UpperCaseTextArea, that
extends JTextArea and converts typing and pasting into UPPER CASE.
Perhaps one of you guys can provide that. It's likely that many
other people would find that useful.
Thanks,
Steve
stephen@basit.com
------------------------------------------------------------------------------------------------
import javax.swing.JTextField;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
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);
}
}
}
//---- end--------