Please, l need help on how to code situation that all JTextFields in JFrame is filled before the save JButton is enable. A kind of validation which make sure that use input values into JTextFields. Thanks
I think it's more common to do that validation after the user presses the Save button - at least if there are quite a number of textfields in the form. The checking routine could then also tell the user which field(s) are missing input.
If, however, there are only let's say two fields in the form, then you could attach a focusListener or inputVerifier to each field and check both fields on focusLost.
Add a DocumentListener to all the fields. On each insert or remove call run checkEmpty() method. In the method iterate through list of the JTextFields and fill one boolean flag isAllFieldsFilled. Depending on the flag enable/disable the button.
Thanks StanislavL for finding out time to respond. I tried addDocumentListener on all the field but anytime l type a single character it will enable the save button and that is not what l want. l want a situation where all the field is filled first before the save button is enable and if by any chance a field is empty by a user the save but will go back to disable mode. l will be glad if you can show me some code that will do that. Thanks
Thanks Stanislav, l was able to done it. The problem was really in checkEmpty method, l was setting focus on the textfield, immediately commented the line that setfocus and called in it in the insertUpdate() and removeUpdate() methods of DocumentListener it works. Below is the code. But if you have a better way of doing it l will appreciate and l also want to check and ensure that textfield accept string only, how do l do it?