• Advertisement

Archived

This topic is now archived and is closed to further replies.

[java] Quick Swing Question.

This topic is 5754 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m using jdk 1.3 and I''m trying to set up a text field to only accept letters or the space character. I''ve subclassed the PlainDocument and I can get it to accept only letters but it will not accept the space character at all. When I getNumericValue on the character it returns a -1, it does not seem to recognize the fact that a space character is there even though it fires an insertString call. Even if i just try to copy the characters one at a time without filtering it still will not allow spaces to be inserted.. heres what my code looks like : static class LettersDocument extends PlainDocument { public void insertString(int offset, String str, AttributeSet a) throws BadLocationException { char[] source=str.toCharArray(); char[] dest = new char[str.length()]; int y=0; String result = null; int length = getContent().length(); Character space = new Character('' ''); if (str == null) { return; } System.out.println(" string is :"+":"); //System.out.println("Current string is : "+getContent().getString(0,getContent().length())); Character test = null; for (int x=0;x0) { result=new String(dest); super.insertString(offset,result.trim(),a); } else { return; } } } What am I doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
One of my Co-workers did this type of thing, I''ll check the CVS tomorrow when I''m at work and I''ll see if I can help you out.

Share this post


Link to post
Share on other sites
Will not the trim() function remove leading/trailing whitespace? I haven''t looked into it, really, to see where the insertString gets called, but is it possible that you''re trimming off the space?

Maybe when I get some spare time I''ll look at it more closely... or maybe you already solved the problem. Either way...

-pirate_dau

Share this post


Link to post
Share on other sites
Trim() should get rid of whitespace but I don''t think that its being called anywhere in the code. The strange thing is that the normal PlainDocument seems to work just fine its just when I extend that classes insertString() it seems to have problems. Even the space character that is defined evaluates to ''-1'' (using getNumericValue) and its not inserted either.. I just can''t figure out why a space character would not be evaluated with isWhitespace() to true. I''ve tried (source[x] == '' '') and that did evaluate to true but then...... oh god. I see it now *slaps forehead*.. oh man I feel dumb, yes I am triming the string right before I insert it which (of course as you pointed out) trims off the whitespace.. and if it is only whitespace then nothing gets inserted. Ooook then I''ll go fix that and try not to look too embarrassed Though I would still like to know why the space is evaluating to -1 when it should be something like 10 I think.. oh well as long as it works

Share this post


Link to post
Share on other sites

  • Advertisement