3 Replies - 4677 Views - Last Post: 12 May 2013 - 08:49 PM Rate Topic: -----

#1 jbreen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-May 13

Error: cannot add to layout: constraint must be a string (or null)

Posted 11 May 2013 - 11:33 PM

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. I have a picture of what I have and what I want in attachments.

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)


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

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Error: cannot add to layout: constraint must be a string (or null)

#2 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: Error: cannot add to layout: constraint must be a string (or null)

Posted 12 May 2013 - 12:08 AM

I refuse to learn anything about GridbagLayout, but I have a feeling that this would be easier with Jpanels, flowLayout, and a simple loop. I could be wrong, however.
Was This Post Helpful? 0
  • +
  • -

#3 jbreen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-May 13

Re: Error: cannot add to layout: constraint must be a string (or null)

Posted 12 May 2013 - 12:13 AM

Answered here http://stackoverflow...raint-must-be-a
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Error: cannot add to layout: constraint must be a string (or null)

Posted 12 May 2013 - 08:49 PM

Leave GridBagLayout to GUI builders and write your GUI a more intelligent way
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1