Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JTextArea Special Character and limitation

Posted on 2006-07-04
9
Medium Priority
?
1,292 Views
Last Modified: 2008-01-09
Hi,

Further to advice last week. Can any one assist With the below example

1) how can i avoid special characterlike @%~ from being typed.
2) Secondly If you set limit to 150 characters and try pasting  block of 160 characters it  should only accept the first 150 characters  
    (truncate). This does not happen and it does not accept any block of charcters above 150 characters when pasted.

    JTextComponent textComponent = new JTextField();
    AbstractDocument doc = (AbstractDocument)textComponent.getDocument();
    doc.setDocumentFilter(new FixedSizeFilter(150));
   
    class FixedSizeFilter extends DocumentFilter {
        int maxSize;
   
        // limit is the maximum number of characters allowed.
        public FixedSizeFilter(int limit) {
            maxSize = limit;
        }
   
        // This method is called when characters are inserted into the document
        public void insertString(DocumentFilter.FilterBypass fb, int offset, String str,
                AttributeSet attr) throws BadLocationException {
            replace(fb, offset, 0, str, attr);
        }
   
        // This method is called when characters in the document are replace with other characters
        public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
                String str, AttributeSet attrs) throws BadLocationException {
            int newLength = fb.getDocument().getLength()-length+str.length();
            if (newLength <= maxSize) {
                fb.replace(offset, length, str, attrs);
            } else {
                throw new BadLocationException("New characters exceeds max size of document", offset);
            }
        }
    }

Thanks
Bangviz
0
Comment
Question by:bangviz
  • 3
  • 2
9 Comments
 
LVL 10

Accepted Solution

by:
mukundha_expert earned 80 total points
ID: 17040103
>>how can i avoid special characterlike @%~ from being typed

write a utility to check for special characters when ever the user presses the key ( keyDown event ),

0
 
LVL 1

Author Comment

by:bangviz
ID: 17040239
An example for both cases appreciated thanks
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17040668
you can check the both in the insertString method of the document filter you have posted!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17047804
If you want the user to enter only the alphabets and numbers then you can try something like this in the KeyDown event,

     char ch =' ' ;

     if  ( ( ch >= 'a'  && ch <= 'z' )  || ( ch >='A' && ch <= 'Z' ) || ( ch >= '0' && ch <= '9') )
           return true;


0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17053003
its lot of work or messy in doing it in key event, good to do it in the insertString method of th document or the filter!
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17053005
and ofcourse to have the code in replace method!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question