2

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.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
abiieez
  • 3,139
  • 14
  • 57
  • 110

2 Answers2

7

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.

npinti
  • 51,780
  • 5
  • 72
  • 96
2

Also you could user an index -1 to reset a JComboBox:

JComboBox ctr = (JComboBox) control;
ctrl.setSelectedIndex(-1); //-1 indicates no selection
Alan Teals
  • 451
  • 4
  • 8