This question already has an answer here:
- getting Exception : java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null) 1 answer
I have researched this error and I cannot seem to find a solution. I am trying to create a grid of 800 JButtons with 40 columns and 20 rows. This will eventually be used to control a domino setting up robot I am making that will tip over dominoes. I have already successfully created a grid using GridLayout, but due to the nature of the project, I would like every other row to be offset by half a button. By this I mean like how keys on a computer keyboard are set up. (I would have added a helpful picture of what I am trying to explain, but apparently beginners who have trouble explaining things aren't allowed to add pictures, whatever).
I try to do this by creating a JPanel array of 20 panels called panel. Then I add to the panel the 40 JButtons. Then I use GridBagConstraints to offset every other row. I read that you shouldn't mix awt and swing so that could be the problem, but i don't know. Here is the code, I figured this out from youtube tutorials as I am a very beginner. Forgive me if anything I have said does not make sense. Code:
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
public class OffsetGrid {
public static void main (String [] args){
JFrame Frame = new JFrame();
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout grid= new GridLayout();
GridBagConstraints gbca= new GridBagConstraints();
GridBagConstraints gbcb= new GridBagConstraints();
JPanel[] panel=new JPanel[20];
for (int row=0;row<20; row++){
panel[row]=new JPanel(new GridBagLayout());
gbca.gridx=1;
gbca.gridy=row;
gbcb.gridx=0;
gbcb.gridy=row;
for (int y=0; y<40;y++){
grid=new GridLayout(1,40);
panel[row].setLayout(grid);
JButton[] button = new JButton[40];
button[y]=new JButton();
button[y].setOpaque(true);
panel[row].add(button[y]);
}
if (row%2==0){
Frame.add(panel[row], gbcb);
}
else {
Frame.add(panel[row], gbca);
}
}
Frame.setVisible(true);
Frame.setLocationRelativeTo(null);
Frame.pack();
}
}
error:
Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:426)
at javax.swing.JRootPane$1.addLayoutComponent(JRootPane.java:531)
at java.awt.Container.addImpl(Container.java:1120)
at java.awt.Container.add(Container.java:998)
at javax.swing.JFrame.addImpl(JFrame.java:562)
at java.awt.Container.add(Container.java:966)
at OffsetGrid.main(OffsetGrid.java:38)
Please help me figure out the problem and get it working. Thanks
edit: I am still confused on exactly how to use gridbagconstraints so I don't even know if gridy and gridx are even the right things to use here. Or even if i should use gridbagconstraints. Please offer any suggestions to get the job done. Thanks
edit: this seems to work.
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
public class OffsetGrid {
public static void main (String [] args){
JFrame Frame = new JFrame();
Frame.setLayout(new GridBagLayout());
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout grid= new GridLayout();
GridBagConstraints gbca= new GridBagConstraints();
GridBagConstraints gbcb= new GridBagConstraints();
JPanel[] panel=new JPanel[20];
for (int row=0;row<20; row++){
panel[row]=new JPanel(new GridBagLayout());
gbca.insets=new Insets(0,100,0,0);
gbca.gridy=row;
gbcb.insets=new Insets(0,0,0,0);
gbcb.gridy=row;
for (int y=0; y<40;y++){
grid=new GridLayout(1,40);
panel[row].setLayout(grid);
JButton[] button = new JButton[40];
button[y]=new JButton();
button[y].setOpaque(true);
panel[row].add(button[y]);
}
if (row%2==0){
Frame.add(panel[row], gbcb);
}
else {
Frame.add(panel[row], gbca);
}
}
Frame.setVisible(true);
Frame.setLocationRelativeTo(null);
Frame.pack();
}
}