Page 1 of 2 12 LastLast
Results 1 to 20 of 34
  1. #1
    JavaTrouble is offline Member
    Join Date
    May 2010
    Posts
    16
    Rep Power
    0

    Default Formatting jTexfields

    Hello,

    How do I format texfields in swing to make sure that the user isn't leaving any texfields blank. I've used the isEmpty method once, i think it was last year but i've completely fergot how to use it.

    hope someone can help

    thanks

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Checking against blank input is not called formatting. It is called validating.
    Just compare the input string with the empty string "" using the equals method.

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    If you want to check whether a text field is empty, you can do this:
    Java Code:
    boolean isEmpty = textField.getText().isEmpty();
    If you want to ensure that it doesn't just contain spaces, use the trim() method:
    Java Code:
    boolean isEmpty = textField.getText().trim().isEmpty();

  4. #4
    JavaTrouble is offline Member
    Join Date
    May 2010
    Posts
    16
    Rep Power
    0

    Default

    Do you have a sample code i could have a look, been searching for a while and haven't found any good samples
    thanks

  5. #5
    JavaTrouble is offline Member
    Join Date
    May 2010
    Posts
    16
    Rep Power
    0

    Default

    thanks dlorde, but it doesn't recognise the getText - red line underneath

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    You've just been given code.
    And you could simply have google "java isempty" and taken the first link...

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Then your text fields aren't JTextFields?

  8. #8
    JavaTrouble is offline Member
    Join Date
    May 2010
    Posts
    16
    Rep Power
    0

    Default

    i realised that after i posted the reply, I hadn't refreshed the page. I know i've been given the code but it doesn't work.
    I've googled every possible way for 2 days and havent found the answer.

  9. #9
    JavaTrouble is offline Member
    Join Date
    May 2010
    Posts
    16
    Rep Power
    0

    Default

    Yes they are Jtextfields, i've just renamed them

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Well, then they have a getText() method.
    JTextField has a getText() method.

    The following will compile:

    Java Code:
    JTextField textField = new JTextField("Some strating text");
    String text = textField.getText();

  11. #11
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    @JavaTrouble - why don't you post the code so we can see what the problem is?

  12. #12
    JavaTrouble is offline Member
    Join Date
    May 2010
    Posts
    16
    Rep Power
    0

    Default

    its recognising the getText, I had the jtextfield as focusLost - it required ActionPerformed. So now I just need to put the isEmpty into an if statement so that it gives the user an output if its left blank
    Last edited by JavaTrouble; 05-27-2010 at 02:29 PM.

  13. #13
    JavaTrouble is offline Member
    Join Date
    May 2010
    Posts
    16
    Rep Power
    0

    Default

    The problem I'm having now is that I keep getting "name is empty" error whether its empty or not. what am i doing wrong?


    <code>

    if ( name != null )
    {
    error.setText("name is empty");
    }
    else {
    error.setText("name is not empty");
    }

    </code>

  14. #14
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Uh? What is name? a JTextFields getText() method never ever returns null.

    And the tags are &#91;CODE] and &#91;/CODE]
    Last edited by PhHein; 05-27-2010 at 12:49 PM.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  15. #15
    JavaTrouble is offline Member
    Join Date
    May 2010
    Posts
    16
    Rep Power
    0

    Default

    its not a getText method, its just setText

    name = Jtextfield

    i've renamed the texfield

  16. #16
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Reread Toll's reply #10.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  17. #17
    JavaTrouble is offline Member
    Join Date
    May 2010
    Posts
    16
    Rep Power
    0

    Default

    my god i didnt realise it would be so hard to get an answer from the "experts" after searching it for days on the net.

  18. #18
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    It's hard to give a precise reply to vague questions and various unrelated code snippets.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  19. #19
    JavaTrouble is offline Member
    Join Date
    May 2010
    Posts
    16
    Rep Power
    0

    Default

    vague question?
    How do you prevent the user from leaving empty/blank fields in a swing form?
    What functions do i use? I just want to see something simple as i've search for days and no luck. Please do take into account that it might be something too simple but i am quite new

    Tell me, is the question not clear? if not please let me know

  20. #20
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    That has been answered by dlorde in reply#3 including code. So that cannot be the question. So what is your actual question?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Page 1 of 2 12 LastLast

Similar Threads

  1. Date formatting
    By bikkerss in forum New To Java
    Replies: 13
    Last Post: 05-08-2010, 04:07 PM
  2. Number formatting
    By gandalf5166 in forum New To Java
    Replies: 7
    Last Post: 03-26-2010, 02:54 AM
  3. Formatting String
    By ersachinjain in forum Advanced Java
    Replies: 13
    Last Post: 10-21-2009, 09:26 PM
  4. formatting..
    By sireesha in forum New To Java
    Replies: 16
    Last Post: 06-26-2009, 07:11 PM
  5. Formatting a toString
    By MooNinja in forum New To Java
    Replies: 8
    Last Post: 03-31-2009, 07:32 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
  •