Solved

JTextArea Special Character and limitation

Posted on 2006-07-04
9
1,247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Glassfish admin console not working 1 87
printf performancy 11 101
ejb message driven bean mdb creation steps 2 54
Selenium WebElement Login button findElement 9 14
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

737 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