I have an array of JSliders and JTextfields and I want to place a JTextfield then a JSlider below, move left and place the next JTextfield, Jslider again.
setLayout(new GridBagLayout());
JTextField[] textField = new JTextField[NUM_CHANNELS + 1];
JSlider[] sliders = makeSliders(NUM_CHANNELS + 1);
for (int i = 0; i < NUM_CHANNELS + 1; i++) {
textField[i] = new JTextField();
textField[i].setText("Channel " + (i + 1));
add(textField[i]);
add(sliders[i]);
}
With the above, I get JTextField - JSlider ... in a row.
You need to add GridBagConstraints when you add your components
GridBagConstraints gbc = new GridBagConstraints();
for (int i = 0; i < NUM_CHANNELS + 1; i++) {
gbc.gridy = 0;
textField[i] = new JTextField();
textField[i].setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
textField[i].setText("Channel " + (i + 1));
add(textField[i], gbc);
gbc.gridy = 1;
add(sliders[i], gbc);
gbc.gridx++;
}