In my Swing application, the user must insert numbers and values before switching to the next window. Now that a clean program should, I check every input if it is valid or not, and if not, an error message is displayed and the next window does not open.
The structure of this check is as follows (example):
Button buttonToOpenNextWindow = new JButton("next"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(checkValidty){
(*) Note: I know that the principle of multiple JFrames is ugly, and I have to change it, but for this question it does not matter.
Now the focus of this question is checkValidity() , which I structured as follows:
private boolean checkValidity(){ // check input 1 try{ Integer.parseInt(textField1.getText()); }catch (NumberFormatException e){ new ErrorDialog("input 1 is invalid!"); // own implemented dialog return false; } // check input 2 try{ Integer.parseInt(textField2.getText()); }catch (NumberFormatException e){ new ErrorDialog("input 2 is invalid!"); // own implemented dialog return false; } // (...) // check input n try{ Integer.parseInt(textField_n.getText()); }catch (NumberFormatException e){ new ErrorDialog("input n is invalid!"); // own implemented dialog return false; } return true; }
This works exactly the way I want, but the code itself is very ugly because, having several input parameters, the method gets 200, 300 or more lines long (since I not only check if this number is there, but if the number makes sense in the context of program logic, etc.). Is there a Swing -own method to test such things? Or does anyone know better how to implement this particular functionality using split methods?
Valentino Ru
source share