How to clear text fields using for loop in java

12,561

Solution 1

Code like this:

private javax.swing.JTextField num1;
private javax.swing.JTextField num2;
private javax.swing.JTextField num3;
private javax.swing.JTextField num4;
private javax.swing.JTextField num5;
private javax.swing.JTextField num6;
private javax.swing.JTextField num7;

Is code that is crying out to be arranged and simplified by using collections or arrays. So if you use an array of JTextField or perhaps better an ArrayList<JTextField>. Then clearing them all is trivial.

public static final int FIELD_LIST_COUNT = 7;

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

// in constructor
for (int i = 0; i < FIELD_LIST_COUNT; i++) {
  JTextField field = new JTextField();
  fieldList.add(field);
  fieldHolderJPanel.add(field); // some JPanel that holds the text fields
}

// clear method
public void clearFields() {
  for (JTextField field : fieldList) {
    field.setText("");
  }
}

Solution 2

You can easily get the components inside the container by container.getComponents() method with consider some important things:

  1. There may another container like JPanel.
  2. There may another component like JLabel,JButton,....

Use this method:

public void clearTextFields (Container container){

  for(Component c : container.getComponents()){
   if(c instanceof JTextField){
     JTextField f = (JTextField) c;
     f.setText("");
 } 
  else if (c instanceof Container)
     clearTextFields((Container)c);
}
}

Call the method like this:

clearTextFields(this.getContentPane());
Share:
12,561
DnwAlgorithma
Author by

DnwAlgorithma

Updated on June 12, 2022

Comments

  • DnwAlgorithma
    DnwAlgorithma almost 2 years

    I created text fields in Java as following. When I click a "clear" button I want to clear all of these text fields at once.

    private javax.swing.JTextField num1;
    private javax.swing.JTextField num2;
    private javax.swing.JTextField num3;
    private javax.swing.JTextField num4;
    private javax.swing.JTextField num5;
    private javax.swing.JTextField num6;
    private javax.swing.JTextField num7;
    

    Now I want to know how to use a for loop to clear these all text fields like:

    for(int i=1;1<7;i++){
       num[i].settext(null);
    }
    
    • Bart
      Bart almost 11 years
      Create an array of JTextField instances and loop over that one.
    • Andrew Thompson
      Andrew Thompson almost 11 years
      1) "How can i do this???????" First fix that stuck '?' key! 2) Please use the correct spelling for words like 'you', 'your' & 'please'. This makes it easier for people to understand and help. 3) Please add an upper case letter at the start of sentences. Also use a capital for the word I, and abbreviations and acronyms like JEE or WAR. This makes it easier for people to understand and help.
  • MadProgrammer
    MadProgrammer almost 11 years
    Why I would normally agree, what happens if the fields don't live in a single container?
  • Azad
    Azad almost 11 years
    @MadProgrammer: Nothing happens, it's just the matter of design and separating the work area.
  • DnwAlgorithma
    DnwAlgorithma almost 11 years
    Sorry, I'm using this on netbeans(with using interface palette).So I cannot initialize textfields as an array when it creating.So then how?
  • DnwAlgorithma
    DnwAlgorithma almost 11 years
    Sorry, I'm using this on netbeans(with using interface palette).So I cannot initialize textfields as an array when it creating.So then how?
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels almost 11 years
    @user2605013: then just put them in an ArrayList as I've already suggested. Note that you're far better off not using a code generation tool and instead creating your Swing code by hand til you understand Swing and Java.
  • Azad
    Azad almost 11 years
    @user2605013: I knew you're using Netbeans form, this method not need to create an array or List of JTextFields, it will do all the works only copy and paste it, but take my advice go and learn Swing by yourself, it's amazing, you'll find many interesting things ;).