4

I have a JPanel that have a lot of JTextFields and JComboBoxes and JRadioButtons, so i want to make them all in the default values in one shot.

I used to empty every field one by one but this take lot of time, and maybe i miss some fields, or some times i can add another fields, so it is not practice at all.

public void empty(){
   field1.setText("");
   field2.setText("");
   field3.setText("");
   ...
}

So is there any way to make all the fields empty in one shot?

Thank you.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140

4 Answers4

4

If the JTextFields are not all in the same container, this would be a possible approach:

private List<JTextField> allTextFields = new ArrayList<JTextField>();

private JTextField createNewTextField(String text) {
    JTextField textField = new JTextField(text);
    allTextFields.add(textField);
    return textField;
}

private void resetAllTextFields(){
    for (JTextField textField : allTextFields) {
        textField.setText("");
    }
}

..and then instead of using the constructor JTextField myTextField = new JTextField("content") use JTextField myTextField = createNewTextField("content");

user7291698
  • 1,972
  • 2
  • 15
  • 30
2

Your question is a bit broad, and no one-size-fits all solution is optimal, but I can say that iterating through a JPanel's components, and clearing all is not the best solution for several reasons, including:

  • You may later wish to add a component that is not cleared, or that is only cleared under certain conditions, and having code that directly clears components may make this hard to debug and fix.
  • Most GUI's have layers of JPanels, and if later you add a sub-JPanel, does this mean that you're going to want to recursively iterate through all components and clear them?

Better is to strive to separate concerns, to reduce coupling of the model to the view, and for this reason, likely the cleanest solution is to try to separate your model from your view, à la MVC for instance, clear portions of the model that need to be clear, and in the control clear only those portions of the view bound to that section of the model.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

This should work:

Component[] tmp = p.getComponents(); // p is your JPanel

for(Component c : tmp) {
    if(c instanceof JTextField) {
        ((JTextField) c).setText("");
    }
}

and you can even do different code for different component types...

Bruno Zamengo
  • 800
  • 1
  • 11
  • 24
1

The link How to clear all input fields within a JPanel i think it help me, my code should look like this :

private void clearAllFields() {

    for (Component C : myPanel.getComponents()) {
        if (C instanceof JTextField || C instanceof JTextArea) {
            ((JTextComponent) C).setText(""); 
        }

        if (C instanceof JComboBox) {
            ((JComboBox) C).setSelectedIndex(0); 
        }

        if (C instanceof JRadioButton) {
            ((JRadioButton) C).setSelected(false); 
        }

        if(C instanceof JDateChooser){
            ((JDateChooser) C).setDate(null);
        }
    }
}
Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • Sorry, but this does not look clean, as that it uses a very broad brush and suggests that your view (the GUI) is much too closely integrated with your model (the non-GUI logic that underpins your program). Much better to have full knowledge and control of exactly what is cleaned and where such as with uses of collections or especially via MVC. – Hovercraft Full Of Eels Dec 24 '16 at 13:50
  • For instance, what if later you, or someone maintaining and upgrading your program wants to add a component that shouldn't be cleared? Now you're stuck with a messy debug. – Hovercraft Full Of Eels Dec 24 '16 at 13:52
  • Or what if you use sub-components? Are you going to want to search recursively? – Hovercraft Full Of Eels Dec 24 '16 at 14:00
  • this is true, you are totally true of what you said – Youcef LAIDANI Dec 24 '16 at 14:06