I want to override the method setText in my JDoubleField that extends JTextField to filter the parameters to guarantee that the input is parse-able. The filter itself is a short piece of code, but I have no idea what the rest of the contents of the method are, so I wanted to have something like:

 @Overridepublic void setText(String sText){try{Double.parseDouble(sText);} catch(NumberFormatException e){sText="";}// The original method goes here.}

Any idea on how I could be able to do that?

  • super.setText(sText);?– Andrew TobilkoMay 17 at 17:02
  • After your custom code call super.setText(sText)– Hemant PatelMay 17 at 17:03
  • Thank you all for your help and quick responses!– BunnyMay 17 at 17:19

Then you call super to execute the rest of logic.

super.setText(sText);
  • The first portion of your comment is actually incorrect because super() refers to the parent classes constructor with no arguments.– RobOhRobMay 17 at 17:18
  • Thanks, I just type so quick– Mạnh Quyết NguyễnMay 17 at 17:19

If you call super.setText() after your custom code, it will run the original method that you overrided.

@Overridepublic void setText(String sText){try{Double.parseDouble(sText);} catch(NumberFormatException e){sText="";}// The original method goes here.super.setText(sText);}

    Your Answer

     

    By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.