Reputation: 3766
Is it to possible to capitalize the FIRST letter in a Textfield
E.g. The user would type 'hello' and 'Hello' would appear in the Textfield.
I fined this code to capitalize all letter http://www.java2s.com/Tutorial/Java/0240__Swing/FormatJTextFieldstexttouppercase.htm
and I try to edit it to capitalize only the FIRST letter put it is wrong
this is my edit
public class UppercaseDocumentFilter extends DocumentFilter {
public void insertString(DocumentFilter.FilterBypass fb, int offset, String text,AttributeSet attr) throws BadLocationException {
fb.insertString(offset, text.substring(0, 1).toUpperCase() + text.substring(1), attr);
}
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,AttributeSet attrs) throws BadLocationException {
fb.replace(offset, length, text.substring(0, 1).toUpperCase() + text.substring(1), attrs);
}
}
Upvotes: 1
Views: 2290
Reputation: 121
This code sample does uppercase replacing for first char in a ComboBox editor. It can be used for any text editor, just change the comboTextEditor variable to point to your text editor. It also does the job in the case whole editing text is selected (case in which typing a char replaces the selection). Code also checks for max length and does not allow more text than allowed by maximum size.
class TextSizeFilter extends DocumentFilter {
int maxCharacters;
boolean capitalizeFirstLetter;
public TextSizeFilter(int maxChars, boolean capitalizeFirstLetter) {
this.maxCharacters = maxChars;
this.capitalizeFirstLetter = capitalizeFirstLetter;
}
public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {
//This rejects the entire insertion if it would make
//the contents too long. Another option would be
//to truncate the inserted string so the contents
//would be exactly maxCharacters in length.
// The insertString(...) method of the DocumentFilter is only called when you use the Document.insertString(...) method to directly update the Document.
// We will not use that type of update. But anyway, the easiest way is to do a replace
replace(fb, offs, 0, str, a);
}
public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException {
//This rejects the entire replacement if it would make
//the contents too long. Another option would be
//to truncate the replacement string so the contents
//would be exactly maxCharacters in length.
// if field content starts with ERROR_MESSAGE_START, do not count this error message as it comes from validation. so keep it.
if ( str.startsWith(ERROR_MESSAGE_START) ) {
String docText = fb.getDocument().getText(0, fb.getDocument().getLength());
String newStr = docText.substring(0, offs) +str + docText.substring(offs + length);
String errorMessage = extractErrorMessage(newStr);
if ( (fb.getDocument().getLength() + str.length() - length - errorMessage.length() ) <= maxCharacters ) {
super.replace(fb, offs, length, str, a);
} else {
Toolkit.getDefaultToolkit().beep();
}
} else {
if ( (fb.getDocument().getLength() + str.length() - length) <= maxCharacters ) {
if (capitalizeFirstLetter) {
if (fb.getDocument().getLength() == 0) {
StringBuilder sb = new StringBuilder(str);
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
str = sb.toString();
// one char typed over a selcted text, where selected text represents the whole editor text
} else if (str!=null && str.length() == 1 && comboTextEditor.getSelectedText() != null && comboTextEditor.getSelectedText().length() == fb.getDocument().getLength() ) {
StringBuilder sb = new StringBuilder(str);
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
str = sb.toString();
}
}
super.replace(fb, offs, length, str, a);
} else {
Toolkit.getDefaultToolkit().beep();
}
}
}
}
Upvotes: 0
Reputation: 347314
You're on the right direction, you might have a look at fb.getDocument().getLength()
to determine the current length of the Document
, when it's 0
, update the first character of the text
You might then be able to use something like...
String text = "testing";
StringBuilder sb = new StringBuilder(text);
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
text = sb.toString();
System.out.println(text);
to capitalize the first character of the input text
. You might want to do some other checks, but that's the basic idea
Seems to work okay for me
public class UppercaseDocumentFilter extends DocumentFilter {
public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
if (fb.getDocument().getLength() == 0) {
StringBuilder sb = new StringBuilder(text);
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
text = sb.toString();
}
fb.insertString(offset, text, attr);
}
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
if (fb.getDocument().getLength() == 0) {
StringBuilder sb = new StringBuilder(text);
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
text = sb.toString();
}
fb.replace(offset, length, text, attrs);
}
}
Upvotes: 2