Solved

JTextField Document for integers

Posted on 2010-09-01
3
800 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 88
couple of eclipse 5 46
servlet example issue 6 46
hibernate example issues from command prompt 10 50
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

821 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