-1

I'm working to a Java project at uni and I have to stay loyal to object orientation as much as I can. So I have a doubt.

I have many windows with text fields and buttons and everytime I click on a button I need to check whether the text fields in frames are empty.

Now what I did was adding a new method in ActionListener classes and called it check(). This method checks what I said and if there are empty text fields it throws an exception that I created (let's say EmptyFieldsException).

Then I have a try/catch block in actionPerformed, calling the check method. If the exception is catched then a JOptionPane pops up.

My question is: is this a good way to handle such a problem (using exceptions)? Or is it actually overkill? Maybe I could have simply used an if statement in actionPerformed, I'm a bit confused about this.

Silvia
  • 9
  • 4
  • IMHO do not use Exceptions for that. Exceptions should be used for exceptional cases, (unfortunately?) invalid input is not that exceptional. Exceptions can be used when later processing the input, kind of to remind you have forgotten to check the input, but not when checking the input. – user85421 Jan 28 '17 at 15:06
  • Exceptions are slow and should not be used for ordinary checking. In particular they should not be used for something as unexceptional as an empty text field. Use a simple `if(myTextField.getText.length() == 0)` or similar. – rossum Jan 28 '17 at 15:12
  • @rossum You're missing the brackets on `getText()`. – byxor Jan 28 '17 at 15:35
  • Possible duplicate of [Java - Check if JTextField is empty or not](http://stackoverflow.com/questions/17132452/java-check-if-jtextfield-is-empty-or-not) – byxor Jan 28 '17 at 15:36
  • @Brandon. Whoops! Thanks for the correction (though I did say "or similar" :) ) Better, `if(myTextField.getText().length() == 0)` or similar. – rossum Jan 28 '17 at 15:41

1 Answers1

-1

You should stick with creating a method that checks all the text fields using if statements and returns a boolean.

private boolean checkFields(){
        if(textField1.getText().lenght()<1)
            return false;
        if(textField2.getText().lenght()<1)
            return false;
        //check all text fields

        return true;
}

The method representing your button action will first call the checkFields() method and if true is returned continue else return after it informs the user that not all text fields were filled.

private void buttonAction(){
        if(!checkFields()){
            Alert message=new Alert(        Alert.AlertType.WARNING);
            message.setHeaderText("Text field empty");
            message.setContentText("One of the text fields is empty");
            message.showAndWait();
            return;
        }

        //continue the method
    }

If you ensure that the method will only run if all the TextFields have text there is nothing to worry. Exceptions are much more costly to throw and catch and should be used in exceptional cases.

Alexandru Cancescu
  • 829
  • 1
  • 9
  • 19