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
Printable View
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
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.
If you want to check whether a text field is empty, you can do this:If you want to ensure that it doesn't just contain spaces, use the trim() method:Code:boolean isEmpty = textField.getText().isEmpty();
Code:boolean isEmpty = textField.getText().trim().isEmpty();
Do you have a sample code i could have a look, been searching for a while and haven't found any good samples
thanks
thanks dlorde, but it doesn't recognise the getText - red line underneath
You've just been given code.
And you could simply have google "java isempty" and taken the first link...
Then your text fields aren't JTextFields?
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.
Yes they are Jtextfields, i've just renamed them
Well, then they have a getText() method.
JTextField has a getText() method.
The following will compile:
Code:JTextField textField = new JTextField("Some strating text");
String text = textField.getText();
@JavaTrouble - why don't you post the code so we can see what the problem is?
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
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>
Uh? What is name? a JTextFields getText() method never ever returns null.
And the tags are [CODE] and [/CODE]
its not a getText method, its just setText
name = Jtextfield
i've renamed the texfield
Reread Toll's reply #10.
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.
It's hard to give a precise reply to vague questions and various unrelated code snippets.
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
That has been answered by dlorde in reply#3 including code. So that cannot be the question. So what is your actual question?