Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Please help

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Please help

    I have a big problem when creating the calculator in Jcreator.the thing is after running the program, when I press the decimal point for anytime(even more than once) it displays in the text field.I tried to solve this & failed.please can some one give me a solution for this.It's better if you can provide me the codings.

  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Please help

    If you want to ignore the decimal point key if there is already a decimal point displayed in the text field, use a DocumentFilter, something like this:
    ...
    JTextField textField = new JTextField();
    PlainDocument doc = new PlainDocument();     // or whatever document type you require
     
    doc.setDocumentFilter(new DocumentFilter() {
        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
                throws BadLocationException
        {
            // get document text
            Document currentDoc = fb.getDocument();
            String docText = currentDoc.getText(0, currentDoc.getLength());
     
            // return if both current and new text have a decimal point
            if (text.contains(".") && docText.contains(".")) {
                System.out.println("Decimal point already entered!");
                return;
            }
            // allow the new text
            super.replace(fb, offset, length, text, attrs);
        }
    });
    textField.setDocument(doc);
    If that's not what you wanted, you'll have to explain a bit more clearly.

  3. The Following User Says Thank You to dlorde For This Useful Post:

    prasansani (May 30th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Please help

    Quote Originally Posted by prasansani View Post
    I have a big problem when creating the calculator in Jcreator.the thing is after running the program, when I press the decimal point for anytime(even more than once) it displays in the text field.I tried to solve this & failed.please can some one give me a solution for this.It's better if you can provide me the codings.
    Well, you could try to get it to reset the TF to zero if it's pushed more than once or something.

    Also, what's wrong with that? Just make sure it'll output "Error" or something if the user does that.