Is there are a method to clear the value of a Swing component. For example if we take the JTextField every time I want it to be cleared I have to call - txtField.setText(""). I once made a utility method for these type of cases -
public static void clearFields(JComponent[] components) {
for (int i = 0; i < components.length; i++) {
JTextComponent jComponent = (JTextComponent) components[i];
jComponent.setText("");
}
}
I want to know if a better way is available in the Swing API itself. Or whether there is a way to refresh all the values in a JPanel.
there are two ways
nothing wrong with your way, but I'd suggest to test
if (c instanceof JTextField) {, then you can to re_set (differen too) value for differentJComponents(or their group of) in one loopput desired
JComponentsto the some type ofarrayormap,notice remove (if is there)
DocumentorDocumentListenerfrom allJTextComponentbefore, thenafter value is re_seted, loop ended, then add requied listener back toJTextComponent, to avoid to firing recrusive events ...