Solved

JTextField Document for integers

Posted on 2010-09-01
3
799 Views
Last Modified: 2012-06-21
The class below is used by a JTextField and is intended to allow integers only. However, what it does is allow only integers >= 0 because it will not allow a "-" to be typed for a negative integer. How can I get it to accepts negative integers, too?

/**
 * This document only allows integer values to be added to it.
 */
static class IntegerDocument extends PlainDocument {

	public void insertString(int offset, String string, AttributeSet attributeSet) throws BadLocationException {

	if (string != null) {
		try {
			Integer.decode(string);
			super.insertString(offset, string, attributeSet);
		}
		catch (NumberFormatException ex) {
			System.out.println ("not a digit");
			Toolkit.getDefaultToolkit().beep();
		}
	} // end if
}
} // end IntegerDocument

Open in new window

0
Comment
Question by:allelopath
3 Comments
 
LVL 6

Accepted Solution

by:
Protuhj earned 250 total points
ID: 33580404
use Integer.parseInt() rather than decode... decode can only take positive numbers..

http://forums.sun.com/thread.jspa?threadID=5437277
0
 
LVL 1

Author Comment

by:allelopath
ID: 33580761
Good idea, but that doesn't work, at least in my case. The insertString() method is called every time a character is typed, so if the user intends to enter "-1", insertString() is called for the - , and both decode() and parseInt() throw the NumberFormatException.

I guess what I have to do is check for the -, like so:

if (string != null) {
      try {
            // let minus sign pass through
            if (! string.equals("-")) {
                  Integer.decode(string);
            }
            super.insertString(offset, string, attributeSet);
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 33582529
you should be parsing the result string, not just the string entered
and you need to treat "-" as a special case
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why cannot we forward request once the response is committed 2 42
arguments to jar 5 26
spring jars download 1 27
String array comparison 4 34
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

777 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