2

I've been working this for 2 days but I can't still figure how to check if the jtextfield is empty (Double not String) before passing it to my database.

I figured it out how to validate String if the field is empty, but I need to put the right code on how to validate Double if the field is empty.

Thanks in advance.

Here's my code:

private void saveButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    String inventcodef = inventCodeField.getText();
    String inventnamef = inventNameField.getText();
    String categ = cmbname.getSelectedItem().toString();
    double inventreorderf = Double.parseDouble(inventReorderField.getText());

    ..............
    if ((inventCodeField.trim().Length()==0) ||       (inventNameField.trim().Length()==0)
mix
  • 137
  • 2
  • 3
  • 12
  • 1
    How about `inventReorderField.getText().trim().length == 0` or `inventReorderField.getText().isEmpty()` condition? But still doesn't guarantee that entered value is double parsable, Better to use JFormattedTextField. – Harry Joy Feb 22 '12 at 06:13
  • Hello Harry joy/ I just usd inventReorderField.getText().trim().length == 0 it says an error and inventReorderField.getText().isEmpty() but it doesn't return anything. thank you! – mix Feb 22 '12 at 06:16
  • thanks harry I'll try the JFromattedTextField :) – mix Feb 22 '12 at 06:21
  • 1
    Never do getText().trim().length, with JTextField or anything related to that, always use getDocument().getLength(), then do your stuff on that. – nIcE cOw Feb 22 '12 at 06:47
  • @GagandeepBali Hello, please correct me if I'm wrong. is getDocument().getLength() use for validating if the JTextField is empty? Thank you! – mix Feb 22 '12 at 09:07
  • @mix : True it is used to know that thing. If the result is > 0, then JTextField is not empty, else it is :-) – nIcE cOw Feb 22 '12 at 09:21
  • thank you so much! :) I'll try this one :) is it working with DOUBLE? – mix Feb 22 '12 at 11:36
  • @GagandeepBali hello. I tried inventReorderField.getDocument().getLength()==0 (it is double) but it doesn't work but for string it works fine :) – mix Feb 22 '12 at 11:47
  • @mix : The thing I told you is used to check the length of the stuff inside the input Fields, i.e. JTextField, JTextArea etc. For checking Double I guess the answer you selected is the right one JFormattedTextField can do that part. – nIcE cOw Feb 22 '12 at 11:52
  • @mix : Your Welcome and Keep Smiling :-) – nIcE cOw Feb 22 '12 at 13:49
  • Hi @GagandeepBali i have problem , how can I save / declare the jformatted text? if i'll replace the double inventreorderf = Double.parseDouble(inventReorderField.getText()); – mix Feb 22 '12 at 17:02
  • @mix : Yeah true, that's the way to go :-) – nIcE cOw Feb 22 '12 at 17:09
  • @mix : Or you can do like this, try {Document document = inventReorderField.getDocument(); int len = document.getLength(); double inventreorderf = Double.parseDouble(document.getText(0, len));} catch(BadLocationException ble){ble.printStackTrace();} – nIcE cOw Feb 22 '12 at 17:17
  • hi @GagandeepBali should I put them inside private void saveButton3ActionPerformed(java.awt.event.ActionEvent evt)? I'm sorry I'm just a student in java. You're help is very much appreciated. I'm thankful :) – mix Feb 22 '12 at 17:25
  • @mix : Exactly inside the code that you had written in your question, too true saveButton3ActionPerformed(...). No worries, I am always there to help :-) Your Welcome. – nIcE cOw Feb 22 '12 at 17:31
  • @GagandeepBali it says BadLocationException cannot find symbol :) – mix Feb 22 '12 at 17:33
  • @mix : import javax.swing.text.BadLocationException, at the start of your program. – nIcE cOw Feb 22 '12 at 17:56
  • @GagandeepBali it works now! thank you so much! I've been searching for this for a couple of days :) – mix Feb 22 '12 at 18:05
  • On Stack Overflow, it is not necessary to tag a question as solved. By marking an answer accepted, that's enough to designate your question as solved, so I've rolled back your subsequent edit. – BoltClock Feb 22 '12 at 18:14
  • Your Welcome and Keep Smiling :-) – nIcE cOw Feb 22 '12 at 18:30

2 Answers2

4

To enforce formatting (numeric etc) you can use JFormattedTextField.

To ensure values are not blank see No blanks in JTextField

Community
  • 1
  • 1
Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
  • Correct me if I'm wrong but I think JFormattedTextField (for numeric) is used to validate if the the value entered is NUMERIC or not. Does it detect if the user didn't input any value? thank you so much – mix Feb 22 '12 at 06:21
  • exactly, that's why DOUBLE JTEXTFIELD in your subject looks strange – Oleg Mikheev Feb 22 '12 at 06:24
2

You are reading the double at first as a String. So, you can do something like this:

double inventreorderf;
if (inventReorderField.getText().trim().length == 0)
{
    //Do something which should happen when the field is empty
}
else
{
    try
    { 
        inventreorderf = Double.parseDouble(inventReorderField.getText());
    }

    catch (Exception e)
    {
        //The user has entered an invalid number. Notify him/her here.
    }
}
npinti
  • 51,780
  • 5
  • 72
  • 96
  • but where should I put those code? does it need to be inside the SAVEBUTTONACTION? sorry I'm new in java. :) – mix Feb 22 '12 at 06:37
  • @mix: Yes this needs to be put in the event handler, instead of this line: `double inventreorderf = Double.parseDouble(inventReorderField.getText());`. Note that to my knowledge the `JFormattedTextField` only checks of the format of the input, and not if the field is empty or not. – npinti Feb 22 '12 at 06:44
  • Does it works even if I change jtextfield into jformattedtext? – mix Feb 22 '12 at 07:05
  • @mix: The `JFormattedText` extends the `JTextField`, so you should be able to get it to work. – npinti Feb 22 '12 at 07:18