Solved

JTextField Document for integers

Posted on 2010-09-01
3
801 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
spring jars download 1 65
Running JavaFX on JDeveloper 12C 1 104
Android development question 2 103
how to debug htl and js pages 8 75
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
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.
Suggested Courses

740 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