abiieez abiieez - 1 year ago 88
Java Question

How to clear all input fields within a JPanel

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

, etc.

Answer Source

Maybe something like so:

for(JComponent control : parentPanel.getComponents())
    if(control instanceof JTextField)
        JTextField ctrl = (JTextField) control;
    else if (control instanceof JComboBox)
        JComboBox ctr = (JComboBox) control;

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download