Note that there are some explanatory texts on larger screens.

plurals
  1. POEfficient way to add lots of different swing objects to a panel/frame. (Java)
    text
    copied!<p>I'm trying to do as the title says.</p> <p>I've got an efficient way for posting several of the same swing objects to a frame by storing them in an array and adding them using a for loop like so:</p> <pre><code>JLabel[] contrllabels= new JLabel[8]; contrllabels[0] = new JLabel("SCF Type: "); contrllabels[1] = new JLabel("Units: "); contrllabels[2] = new JLabel("Spherical Harmonics: "); contrllabels[3] = new JLabel("Molecular Charge: "); contrllabels[4] = new JLabel("PP: "); contrllabels[5] = new JLabel("DFT Type: "); contrllabels[6] = new JLabel("Max Iterations: "); contrllabels[7] = new JLabel("Mult: "); for(int i = 0;i&lt;contrllabels.length;i++){ c.gridy = i; frame.add(contrllabels[i],c); } </code></pre> <p>But what if there are several swing objects of different types? I've got several combo boxes and textfields which I'd like to be added to the frame in a similar manner. I use gridbaglayout so if I don't use a for loop, I end up with lots of unnecessary code due to giving the constraints new values every time I want a different object added.</p> <p>Is there such thing as an array of references which point to these different objects which I can then iterate through to add to the frame? Something like</p> <pre><code>JTextField tf = new JTextField(5); JComboBox cb = new JComboBox("example"); Swing[] array = {tf,cb} for(int i = 0;i&lt;array.length;i++){ c.gridy = i; frame.add(array[i],c); } </code></pre> <p>I know such an array doesn't exist, but is there some way of implementing such a thing? It would greatly reduce the number of lines in my code and make it less confusing.</p> <p>Thank you</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload