Solved

JTextArea Special Character and limitation

Posted on 2006-07-04
9
1,231 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 20 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

830 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