Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manipulating PlainDocument

Status
Not open for further replies.

kmcculler

Programmer
Jul 5, 2000
81
US
Hi all,
Hope there's someone out there knowlegable about documents (specifically PlainDocument) that can help me. I'm working on a rather small part of a very large project, but it concerns the use of an extended PlainDocument that keeps track of and changes my data objects (built from an Oracle DBfield).
In the section I'm working on I need to check some rules against the content of that document. (ie username must be between 5 and 22 characters in length) I've thought of insertString or DocumentListeners on my document but they would check the rules anytime that the document was changed. This won't work for several reasons the most obvious is creating a new username... (enter a single letter into a blank document and it fails because its not at least 5 characters)
I also thought of adding a focus listener to the textfield that the document is connected to, so the rule would be checked when focus is lost. That WOULD work exept for the problem of what to do if the test fails (the document already updated the data object with the new text and an undo wouldn't be very user friendly since this is the first notification the user has that the name isn't acceptable)
Is there any way the document can handle some sort of post update or lost focus??? or am I looking at this all wrong? Please let me know any ideas you have... anything could be helpful and would be appreciated. Kris McCuller
Programmer Portiva Corp.
kmcculler@portiva.com
 
Hi kmculler:

I don't understand your question at all, but, you can add a StyledDocument to the TextArea to limit the username size. This class will do that job:

Code:
class LimitedStyledDocument extends DefaultStyledDocument {
    int maxCharacters;

    public LimitedStyledDocument(int maxChars) {
        maxCharacters = maxChars;
    }

    public void insertString(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.
        if ((this.getLength() + str.length()) <= maxCharacters){
			try{
				Integer.parseInt(str);
			}
			catch(NumberFormatException e){
			   if(str.length()>0) Toolkit.getDefaultToolkit().beep();			
			   return;
			}
            super.insertString(offs, str, a);
		}
        else
            Toolkit.getDefaultToolkit().beep();
    }
}

This will keep the username on size, but, I can't figure out how to fix the 5 letter problem. It is a matter of events, do you have a submit button or something similar? or the component will submit data automattically when it lost focus. I didn't understand the events flow of your program, so please be more specific on that.

Hope it helps.
Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
Sorry I wasn't very clear.. thanks for your reply but its not quite what I'm looking for. The above example (5 to 22 chars)is only one of several rules I have to check that are currently written into static methods and really the catch that makes my example difficult is BETWEEN 5 and 22 so I can't preform the check in insert string. If the <5 condition was added to your above example it would now allow a new username to be typed in.
My flow of events is really the point at which Im stuck so I can't describe it to you very much. There isn't a submit button, currently I've put it on a lost focus listener but I'm having problems with that (firing when I don't want it to, nulls causing exceptions). I really wanted the document to be able to handle it instead of a listener, but I can't seem to figure out away for that to happen.
As for how things are SUPPOSED to flow, the checks need to be made after the username is entered, but before the data object is updated. Which is a time that really doens't exist in my first example since my object was being updated in my insert string... I've changed that to the lost focus event now but Im not happy with the way it flows. Hope I explained a little better this time. Let me know.... Kris McCuller
Programmer Portiva Corp.
kmcculler@portiva.com
 
Would it be possible to split the rules up and check some as the person is typing and others when the focus is lost?

For instance, using a listener you could check as the user is typing to see if he goes over 22 characters, and prompt him if he does. Using the focusLost event, you could check if the string is at least 5 characters and prompt for a re-entry before you insert the string in the database.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top