How to clear all input fields within a JPanel

java, swing

Solution

Maybe something like so:

for(JComponent control : parentPanel.getComponents())
{
    if(control instanceof JTextField)
    {
        JTextField ctrl = (JTextField) control;
        ctrl.setText("");
    }
    else if (control instanceof JComboBox)
    {
        JComboBox ctr = (JComboBox) control;
        ctrl.setSelectedIndex(0);
    }
}

This should iterate over each component within the JPanel and check if the component is a `JTextField` or a `JComboBox` and reset accordingly.

Problem

Is there a way to clear all input fields (`JTextField`, `JComnboBox`, etc) after record submission within a `JPanel` ? Currently what I do is, to access to each component and individually use the `setText("")`, etc.

Original source