Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

chek the value of textfield

Posted on 2001-09-13
9
Medium Priority
?
299 Views
Last Modified: 2012-06-27
i have a textfield component in my applet,

i want the input value is stricted to integer, if there is an invalid input value(for example a,b,....), the textfield will forbiden the input.

 and also there is a limit range for the integer(within
-255...255) ,i also want the textfield component can check this.

how can i do this?

code is better

ws1999
0
Comment
Question by:ws1999
[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
9 Comments
 
LVL 4

Expert Comment

by:Oliver_Dornauf
ID: 6479039
You use SWING in your APPLET ?
0
 

Author Comment

by:ws1999
ID: 6479075
no i hope AWT to be more compatible with normal browser?

what event should i use to reponse the input value?

ws1999
0
 
LVL 9

Expert Comment

by:yongsing
ID: 6481250
I have the code for Swing.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:ws1999
ID: 6481270
hi yongsing,

code please? must swing?

ws1999
0
 
LVL 9

Accepted Solution

by:
yongsing earned 150 total points
ID: 6481371
I have two classes here that you may be interested. The first one, UpperCaseField, extends JTextField. It allows you to restrict the number of characters that can be input into the text field. The number of characters to limit is specified as a third argument to the contructor. For example, normally you contruct a JTextField as such: new JTextField(10); where 10 is the number of columns in the text field. For UpperCaseField, the additional argument is supplied as such: new UpperCaseField(10, 8); where the 8 is the number of characters to limit to. Another feature of UpperCaseField is that whatever letter you key in will be converted to upper case immediately.

/**
 UpperCaseField.java
*/
import javax.swing.text.*;
import java.awt.Font;

public class UpperCaseField extends javax.swing.JTextField {

    protected int maxLength = 0;

    public UpperCaseField() {
        this(null, null, 0, 0);
    }

    public UpperCaseField(String text) {
        this(null, text, 0, 0);
    }

    public UpperCaseField(int columns) {
        this(null, null, columns, 0);
    }

    public UpperCaseField(int columns, int maxLength) {
        this(null, null, columns, maxLength);
    }

    public UpperCaseField(String text, int columns, int maxLength) {
        this(null, text, columns, maxLength);
    }

    public UpperCaseField(Document document, String text, int columns, int maxLength) {
        super(document, text, columns);
        this.maxLength = maxLength;
        if ((text != null) && (maxLength != 0) && (text.length() > maxLength)) {
            setText(text.substring(0, maxLength));
        }
    }

    public void setText(String text) {
        if (text == null) {
            super.setText(null);
        } else if (text.length() > maxLength) {
            super.setText(text.substring(0, maxLength).toUpperCase());
        } else {
            super.setText(text.toUpperCase());
        }
    }

    protected Document createDefaultModel() {
        return (
            new PlainDocument() {
                public void insertString(int offset, String string, AttributeSet attributeSet)
                        throws BadLocationException {
                    if (string == null) {
                        return;
                    }
                    if ((getLength() + string.length()) > maxLength) {
                        return;
                    }
                    char[] upper = string.toCharArray();
                    for (int i = 0; i < upper.length; i++) {
                        upper[i] = Character.toUpperCase(upper[i]);
                    }
                    super.insertString(offset, new String(upper), attributeSet);
                }
            }
        );
    }

}

The second class, DigitsField, extends UpperCaseField. This class will limit the input characters to just digits: 0 - 9. Example of use: new DigitsField(10, 8); where the first argument is the number of columns, and the second is the number of characters to limit to.

/**
 DigitsField.java
*/
import javax.swing.text.*;

public class DigitsField extends UpperCaseField {

    public DigitsField() {
        this(null, null, 0, 0);
    }

    public DigitsField(String text) {
        this(null, text, 0, 0);
    }

    public DigitsField(int columns) {
        this(null, null, columns, 0);
    }

    public DigitsField(int columns, int maxLength) {
        this(null, null, columns, maxLength);
    }

    public DigitsField(String text, int columns, int maxLength) {
        this(null, text, columns, maxLength);
    }

    public DigitsField(Document document, String text, int columns, int maxLength) {
        super(document, text, columns, maxLength);
        if (text != null) {
            for (int i = 0; i < text.length(); i++) {
                if (!Character.isDigit(text.charAt(i))) {
                    throw new IllegalArgumentException("text must be all digits");
                }
            }
        }      
    }

    public void setText(String text) {
        if (text != null) {
            for (int i = 0; i < text.length(); i++) {
                if (!Character.isDigit(text.charAt(i))) {
                    throw new IllegalArgumentException("text must be all digits");
                }
            }
        }
        super.setText(text);      
    }

    protected Document createDefaultModel() {
        return (
            new PlainDocument() {
                public void insertString(int offset, String string, AttributeSet attributeSet)
                        throws BadLocationException {
                    if (string == null) {
                        return;
                    }
                    if ((getLength() + string.length()) > maxLength) {
                        return;
                    }
                    for (int i = 0; i < string.length(); i++) {
                        if (!Character.isDigit(string.charAt(i))) {
                            return;
                        }
                    }
                    super.insertString(offset, string, attributeSet);
                }
            }
        );
    }

}
0
 
LVL 1

Expert Comment

by:praburamalingam
ID: 6481615
Hi;
1.Implement keylistener for the textboxes.
2.Keep the textbox value in a temp variable.
2.Get the key code for each key pressed.
3.If key code is other than for numbers, substitute the value for textbox with the temp value stored in variable.
4.If key code is of numbers, append the key pressed value with the temp value and assign it to textbox.
Hope this helps.
0
 
LVL 9

Expert Comment

by:yongsing
ID: 6481805
praburamalingam, you idea will not work, for the following reason:

1. The character keyed will appear in the JTextField before the keyPressed(), keyTyped() and keyReleased() methods are called. If you set the value of the JTextField inside these methods, any unwanted character will appear in the JTextField first before you can erase it. This means you will see a blink in the JTextField.

2. It won't work when you do a paste (CTRL-V).

3. Every time you want to use a new JTextField to accept only numbers, you have to rewrite the key listener code all over again.
0
 
LVL 9

Expert Comment

by:yongsing
ID: 6492229
ws1999, how is it?
0
 

Expert Comment

by:amp072397
ID: 6789158
ws1999:

You have many open questions to which you must return:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=msaccess&qid=20264790
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20256743
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20250343
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20249986
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20187323
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20184015
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20181707
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20180448
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20169990
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=11401958
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20230693
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20194172
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20187856
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20187852
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20187466
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20182301
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20182182
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20181487
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20181301
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20180787
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20180436
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20172201
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20153303

To assist you in your cleanup, I'm providing the following guidelines:

1.  Stay active in your questions and provide feedback whenever possible. Likewise, when feedback has not been provided by the experts, commenting again makes them receive an email notification, and they may provide you with further information. Experts have no other method of searching for questions in which they have commented, except manually.

2.  Award points by hitting the Accept Comment As Answer button located above and to the left of that expert's comment.

3.  When grading, be sure to read:
http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#3
to ensure that you understand the grading system here at EE. If you grade less than an A, you must explain why.

4.  Questions that were not helpful to you should be PAQ'd (stored in the database for their valuable content?even if not valuable to you) or deleted. To PAQ or delete a question, you must first post your intent in that question to make the experts aware. Then, if no experts object after three full days, you can post a zero-point question at community support to request deletion or PAQ. Please include the link(s) to the question(s).
CS:  http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
At that point, a moderator can refund your points and PAQ or delete the question for you. The delete button does not work.

5.  If you fail to respond to this cleanup request, I must report you to the Community Support Administrator for further action.

Our intent is to get the questions cleaned up, and not to embarrass or shame anyone. If you have any questions or need further assistance at all, feel free to ask me in this question or post a zero-point question at CS. We are very happy to help you in this task!


thanks!
amp
community support moderator

2/8
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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

730 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