Search code examples
javaarraysswingfor-loopjtextfield

Using a for loop to set values in jTextFields


        String[] words = str.split("\\+");
        jTextField43.setText(words[0]);
        jTextField44.setText(words[1]);
        jTextField45.setText(words[2]);
        jTextField46.setText(words[3]);
        jTextField47.setText(words[4]);
        jTextField48.setText(words[5]);
        jTextField49.setText(words[6]);
        jTextField50.setText(words[7]);
        jTextField51.setText(words[8]);
        jTextField52.setText(words[9]);
        jTextField53.setText(words[10]);
        jTextField54.setText(words[11]);
        jTextField55.setText(words[12]);
        jTextField56.setText(words[13]);
        jTextField57.setText(words[14]);
        jTextField58.setText(words[15]);

i want,

        for(int i=0;i<15;i++)
        {
              jTextField[i+43].setText(words[i]));
        } 

How can I do?


Solution

  • I'm just guessing here, but it looks as if all those JTextFields have been generated for you by an IDE using some graphical drag'n'drop editor(?)

    If not, then the answer to your question is, don't write your code that way!

    If you did use an IDE to create your UI, every time you drop a JTextField onto your Swing application the IDE is writing some code for you that generates a JTextField Object.

    In the NetBeans IDE it does something like this:

    // Variables declaration - do not modify                     
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    //etc...
    // End of variables declaration     
    

    and elsewhere in a folded section of code marked "Generated Code" there'll be something like:

    jTextField1 = new javax.swing.JTextField();
    jTextField2 = new javax.swing.JTextField();
    
    jTextField1.setText("jTextField1");
    jTextField2.setText("jTextField2");
    
    // plus some layout code that positions your controls...
    

    The GUI isn't smart enough to know that all those JTextFields are somehow related and you would like to conveniently be able to loop through them. If you write the code yourself, instead of dragging and dropping the controls it's as easy as:

    private JTextField[] jTextFields = new JTextField[35]; 
    
    // initialise and set properties for all those JTextFields...
    jTextFields[0] = new JTextField();
    /// etc ...
    

    Where 35 would be the number of JTextFields you need.

    Alternatively, you could still use your IDE to create your GUI with drag'n'drop like you're used to doing and then put references to ALL those IDE generated controls into an array for easy iteration later:

    private JTextField[] jTextFields = new JTextField[35];
    jTextFields[0] = jTextField1;
    jTextFields[1] = jTextField2;
    // etc...
    

    If you would rather use an ArrayList or similar container that is just as simple:

    private List<JTextField> jTextFields = new ArrayList<>();
    jTextFields.add( jTextField1 );
    jTextFields.add( jTextField2 );
    // etc...
    

    In the case of Netbeans, the IDE defies all programming conventions and starts numbering controls at 1 instead of 0, so watch out for off-by-one errors when you're putting jTextField1 into jTextFields[0] and so on. Better still, give all your controls meaningful names!

    I guess the bottom line is don't be afraid to write the code yourself rather than getting your IDE to generate it for you, and there's also no harm in putting your objects into containers if that is convenient to you.

    Here's the relevant FAQ entry for NetBeans regarding arrays of GUI Controls that explains that you need to code it by hand.

    If you're using another IDE, please refer to the relevant documentation to see if the IDE supports a way of creating Arrays or Collections of controls or whether or not you have to write the code yourself.