Solved

chek the value of textfield

Posted on 2001-09-13
9
287 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
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
 

Author Comment

by:ws1999
ID: 6481270
hi yongsing,

code please? must swing?

ws1999
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 9

Accepted Solution

by:
yongsing earned 50 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now