Solved

JTextField Document for integers

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

914 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

21 Experts available now in Live!

Get 1:1 Help Now