Solved

JTextField Document for integers

Posted on 2010-09-01
3
796 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
Comment Utility
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
Comment Utility
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
Comment Utility
you should be parsing the result string, not just the string entered
and you need to treat "-" as a special case
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now