Results 1 to 5 of 5
  1. #1
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Checking textfield text

    For some reason i cant check if a textbox has text in it here is my current code but it wont work
    Java Code:
    	public void actionPerformed(ActionEvent e)
    	{
    		if (e.getSource() == button1)
    		{
    			String refined;
    			if(input.getText().equals(""))//here is the problem it*** it wont work
    			{
    			refined = String.valueOf((int)Math.random());
    			}
    			else
    			{
    			refined = input.getText();
    			}
    			try
    			{
    			int output = (int) s1.getValue();
    			new Pong(output,refined);
    			dispose();
    			}catch(Exception e1){}
    		}
    	}
    }

  2. #2
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: Checking textfield text

    the if statement never returns true even if there is no text in the field

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Checking textfield text

    Have you placed in println statements that would test the code to make sure that your assumptions aren't incorrect?

    For example:

    Java Code:
        public void actionPerformed(ActionEvent e)
        {
            System.out.println("1: inside of actionPerformed");
            if (e.getSource() == button1) {
                System.out.println("2: source == button1");
                System.out.println("3: input.getText() := \"" + input.getText() + "\"");
                String refined;
    
                // I'd test it input.getText().trim().isEmpty()
                if(input.getText().equals(""))//here is the problem it*** it wont work
                {
                   System.out.println("4: input.getText() is empty");  
                   refined = String.valueOf((int)Math.random());
                   System.out.println("5: refined := " + refined);  
                }
                else
                {
                  refined = input.getText();
                }
                try {
                  int output = (int) s1.getValue();
                  new Pong(output,refined);
                  dispose();
                }catch(Exception e1) {
                    e.printStackTrace(); // *** don't leave this empty!!!
                }
            }
        }
    }
    etc...
    Last edited by Fubarable; 06-13-2012 at 11:20 PM.

  4. #4
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: Checking textfield text

    ive checked in my pong class it wont work i even made a debugging program

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Checking textfield text

    Quote Originally Posted by jsobel View Post
    ive checked in my pong class it wont work i even made a debugging program
    Well that's where doing debugging tricks will help -- they will help tell why things aren't happening. If you're still stuck, you'd best show us your debugging attempt and then tell us about the output from your debug statements.

Similar Threads

  1. Save text in Textfield to an array
    By Norah in forum New To Java
    Replies: 1
    Last Post: 06-12-2012, 05:00 PM
  2. Textfield not displaying correct text
    By nksjolinder1 in forum AWT / Swing
    Replies: 6
    Last Post: 10-25-2011, 09:33 PM
  3. checking entered text in a text box?
    By nickburris in forum New To Java
    Replies: 2
    Last Post: 06-24-2011, 02:09 AM
  4. Replies: 2
    Last Post: 06-14-2010, 10:18 AM
  5. accessing the text of textfield of one class in another
    By vasavi.singh in forum Advanced Java
    Replies: 3
    Last Post: 04-05-2009, 02:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •