Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-27-2008, 09:40 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Creating a TextField which only accepts numbers
Code:
import java.awt.Container; import java.awt.Graphics; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.PlainDocument; public class ValidationTestFrame extends JFrame implements DocumentListener { JLabel label = new JLabel("I only accept numbers"); private IntTextField intFiled; public ValidationTestFrame() { setTitle("ValidationTest"); setSize(300, 200); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); Container contentPane = getContentPane(); JPanel p = new JPanel(); intFiled = new IntTextField(12, 3); p.add(intFiled); intFiled.getDocument().addDocumentListener(this); contentPane.add(p, "South"); contentPane.add(label, "Center"); } public void insertUpdate(DocumentEvent e) { setLabel(); } public void removeUpdate(DocumentEvent e) { setLabel(); } public void changedUpdate(DocumentEvent e) { } public void setLabel() { if (intFiled.isValid() ) { int value = intFiled.getValue(); label.setText(Integer.toString(value)); } } public static void main(String[] args) { JFrame frame = new ValidationTestFrame(); frame.show(); } } class IntTextField extends JTextField { public IntTextField(int defval, int size) { super("" + defval, size); } protected Document createDefaultModel() { return new IntTextDocument(); } public boolean isValid() { try { Integer.parseInt(getText()); return true; } catch (NumberFormatException e) { return false; } } public int getValue() { try { return Integer.parseInt(getText()); } catch (NumberFormatException e) { return 0; } } class IntTextDocument extends PlainDocument { public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str == null) return; String oldString = getText(0, getLength()); String newString = oldString.substring(0, offs) + str + oldString.substring(offs); try { Integer.parseInt(newString + "0"); super.insertString(offs, str, a); } catch (NumberFormatException e) { } } } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
TextField vs TextBox (2) JavaForums Java Blogs 0 04-23-2008 03:50 PM
TextField vs TextBox (1) JavaForums Java Blogs 0 04-23-2008 03:50 PM
JSP - getting value from a textfield Java Tip Java Tips 0 12-01-2007 10:58 PM
alignment of textfield in awt nitinborge5 New To Java 2 07-30-2007 01:16 PM
set focus to the textfield paty New To Java 1 07-24-2007 02:32 AM


All times are GMT +3. The time now is 12:33 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org