わかりましたので、これを解決することになりましArrayList
たBoolean[]
。私の例では、列が2つしかないことを知っていました。確信が持てない場合は、2d ArrayList を実行することをお勧めします。
private void updateTableLayout() {
//tableHolderPanel holds the JXTitledPanels
GridBagLayout gbl = (GridBagLayout) tableHolderPanel.getLayout();
List<Boolean[]> occupied = new ArrayList<Boolean[]>();
occupied.add(new Boolean[]{false, false});
int x = 0;
int y = 0;
boolean notPlaced;
for (TableJXPanel table : tableList) {
GridBagConstraints gbc = gbl.getConstraints(table);
notPlaced = true;
while (notPlaced) {
if (!occupied.get(y)[x]) {
//Set true to first cell occupied
occupied.get(y)[x] = true;
if (gbc.gridwidth > 1 && x == 0) {
occupied.get(y)[1] = true;
}
//Add any additional cells and set them.
for (int i = 0; i < gbc.gridheight - 1; i++) {
if (gbc.gridwidth > 1) {
occupied.add(new Boolean[]{true, true});
} else {
occupied.add(new Boolean[]{false, false});
occupied.get(y+1)[x] = true;
}
}
//Add new row for next comparison
if(occupied.get(y)[1])
occupied.add(new Boolean[]{false, false});
//signal that the table was placed
notPlaced = false;
gbc.gridy = y;
gbc.gridx = x;
}
if (x == 0) {
x++;
} else {
x = 0;
y++;
}
}
tableHolderPanel.remove(table);
tableHolderPanel.add(table, gbc);
}
}
これは最適なコードではない可能性があるため、変更すべき点を見つけた場合はコメントを残してください。