0

Hello guys and ladies,

as I let the Eclipse WindowBuilder create me a JPanel with a FormLayout, I wanted to make this creation to be dynamical, because the program I'm writing needs it that way in order to avoid 1000 row long from. I used the following code:

JPanel pData = new JPanel();
pData.setBounds(10, 232, 381, 163);


FormLayout fLayout= new FormLayout(new ColumnSpec[]{}, new RowSpec[]{});

int numCols = 5;
int numRows = 10;

for(int i=1;i<=numCols;i+=2)
{
    fLayout.insertColumn(i, FormFactory.RELATED_GAP_COLSPEC);
    fLayout.insertColumn(i+1, FormFactory.DEFAULT_COLSPEC);
}

for(int j=1;j<=numRows;j+=2)
{
    fLayout.insertRow(j, FormFactory.RELATED_GAP_ROWSPEC);
    fLayout.insertRow(j+1, FormFactory.DEFAULT_ROWSPEC);
}

pData.setLayout(fLayout);
getContentPane().add(pData);

But starting the program, I get a stack of errors starting with:

"The column index 1 must be in the range [1, 0]"

Changing the index in the for-loop(s) simply changes the number in the middle of this error text, but the rest stays the same.

What am I doing wrong? Is it even possible to create a FormLayout dynamically? I'd really appreciate your help!

Additional Information: The reason I'm using a FormLayout is the fact, the columns have different sizes. I know GridBagLayout can do so as well, but it needs many more lines and numbers to have the same result concerning insets and position. But if it's the only sensible alternative, I'll accept it ... as long as it's dynamical ;-)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Stacky
  • 875
  • 9
  • 24
  • 1
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) `pData.setBounds(10, 232, 381, 163);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. For a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space, to organize the components. – Andrew Thompson May 12 '13 at 17:04
  • Thanks Andrew for your quick response! 1) I'll remember the SSCCE next time! This is my first post ever. I'm learning <:-) 1a) How can I make this an SSCCE? I thought this is short enough already... I don't know how to make it even simplier without making the problem more wage. 2) pData.setBounds(...) was created by the WindowBuilder. I didn't set this. But you're right. I'll make it flexible. – Stacky May 12 '13 at 17:50
  • Would this [`GroupLayout`](http://stackoverflow.com/a/14858272/230513) be an alternative? – trashgod May 12 '13 at 17:58
  • Hmm, I've never used it before, so I need a bit of trial and error to find out what it can and what I can do with it. Important besides the style is the ease I can access the added elements afterwords. – Stacky May 12 '13 at 18:16

1 Answers1

2

It has to do with how the "insertRow()/insertColumn()" work. To insert something you must already have row/columns to insert in between. You should instead use the ".appendRow()/.appendColumn()" which just add a new row or column at the bottom of any existing rows or to the right of any existing columns.

EX:

int numCols = 2;
int numRows = 10;

for(int i=1;i<=numCols;i++)
{
    fLayout.appendColumn(FormFactory.RELATED_GAP_COLSPEC);
    fLayout.appendColumn(FormFactory.DEFAULT_COLSPEC );
}

for(int j=1;j<=numRows;j++)
{
    fLayout.appendRow(FormFactory.RELATED_GAP_ROWSPEC);
    fLayout.appendRow(FormFactory.DEFAULT_ROWSPEC);;
}
this.setLayout(fLayout);

This would add 4 columns(2 default and 2 related gaps) and 4 rows(2 default and 2 related gaps) to whatever already exists.

Jeff
  • 674
  • 1
  • 5
  • 17