Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Jtextfield Validation

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Jtextfield Validation

    Hi all

    In my Swing application i am using Jtextfields.If Jtextfield have empty string("") i need to set it to 0 always .Is there any easy way to set this without if else comparison.

    Thanks

    Nimish


  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: Jtextfield Validation

    Why dont you try JFormattedTextField instead?

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Jtextfield Validation

    hi relixus
    i am newbie here. Can u give me some example how to use JFormattedTextfield for this.

    Thanks

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Jtextfield Validation

    Why can't you use if/else?

    Just to clarify, if the text is deleted out of the textfield, you want it to automatically update to 0?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Jtextfield Validation

    Hello PF
    Really thats i want .. I want to automatically updated into 0.

    Thanks

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Jtextfield Validation

    Does this need to check in real-time? When the user is typing/deleting text from the field or does it check when a button is pressed?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #7
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Jtextfield Validation

    Hi PF

    i need to check in when the user is typing/deleting. Plz help me to find a solution ,,Thanks

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Jtextfield Validation

    Have a play with this code:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
     
    public class TextValidation {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */	
    	public static MyTextField t = new MyTextField(16);
     
    	public static void main(String[] a) {
     
    		JFrame f = new JFrame("JAVA FORUMS");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		//MyTextField t = new MyTextField(16);
    		t.setText("0");
    		f.getContentPane().add(t, BorderLayout.CENTER);
    		f.pack();
    		f.setVisible(true);
    	}
     
    	private static class MyTextField extends JTextField implements
    			ActionListener, DocumentListener {
     
    		public MyTextField(int l) {
    			super(l);
    			addActionListener(this);
    			Document doc = this.getDocument();
    			doc.addDocumentListener(this);
    		}
     
    		//Enter key pressed
    		public void actionPerformed(ActionEvent e) {
    			//System.out.println(getText());
    		}
     
    		//Text inserted
    		public void insertUpdate(DocumentEvent e) {
    			//System.out.println(getText());
    		}
     
    		//Text deleted
    		public void removeUpdate(DocumentEvent e) {
    			//System.out.println(getText());
    			if(getText().equals("")){
    				System.out.println("IM BLANK!");
    			}
    		}
     
    		public void changedUpdate(DocumentEvent e) {
    			//System.out.println(getText());
    		}
     
    	}
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  9. #9
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Jtextfield Validation

     public void removeUpdate(DocumentEvent e) {
                //System.out.println(getText());
                if(getText().equals("")){
                  setText("0");
                }
            }

    I tried the above .But i got Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification.I Google it. Using DoucmentFilter may slove the problem.

    public class Doc extends DocumentFilter {
        public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException
        {
     
            super.insertString(fb, offset, string, attr);
        }
     
          public void remove(FilterBypass fb, int offset, int length) throws BadLocationException
        {
    //    
     
            super.remove(fb, offset, length);
           if(fb.getDocument().getLength()==0)
           {
               insertString(fb, offset, "0", null);
     
           }
     
        }
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException
        {
    //      super.replace(fb, offset, length, text, attrs);
             if(fb.getDocument().getText(0, 1).equals("0"))
            {
                remove(fb, 0, 1);
            }
     
        }
    }

    But dont know it's the exact way of doing this .Plz let me know if u have any suggestions.

    Thanks

Similar Threads

  1. Code Validation help??
    By kmh90210 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 30th, 2010, 11:25 AM
  2. Exception during xml validation
    By vijeta in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 6th, 2010, 02:50 AM
  3. IPv6 validation
    By subhvi in forum Java Networking
    Replies: 1
    Last Post: November 27th, 2009, 07:19 AM
  4. Input Validation
    By nic in forum AWT / Java Swing
    Replies: 4
    Last Post: November 18th, 2009, 10:54 AM
  5. Problems with If validation
    By websey in forum Loops & Control Statements
    Replies: 1
    Last Post: November 18th, 2009, 09:43 AM