Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » draw2d GridLayout - different size of last row(s)(draw2d layout manager size issue)
draw2d GridLayout - different size of last row(s) [message #1067869] Thu, 11 July 2013 01:51 Go to next message
Jordan Deyton is currently offline Jordan DeytonFriend
Messages: 10
Registered: July 2013
Junior Member
Newbie here coming from a slightly-less-newbish SWT background. Smile

I've been having a problem where the last row in a GridLayout gets squished when re-sizing the shell. Source code excerpt is below the following description.

What I am trying to do is display a rectangular (square, actually) grid of figures. This works well except for the last row, which is fine when my application starts, but after re-sizing the shell, the last row tends to become smaller than the other rows in the grid. Even if you restore the dimensions of the shell to the original size, the last row remains squeezed.

My excerpt below is basically creating a square grid (in this case, one-eighth of a larger grid). Creating a grid

// Create a new root figure.
final Figure grid = new Figure();
grid.setFont(this.getParent().getFont());
grid.setForegroundColor(GRIDLINE_COLOR);
grid.setBackgroundColor(BACKGROUND_COLOR);
		
// The GridLayout will vary depending on the symmetry.
GridLayout gridLayout = null;
		
// Get the first row and the number of columns.
int firstRow = (int) Math.floor(size/2.0);
int columns = (int) Math.ceil(size/2.0);
	
// The GridLayout will by columns*columns.
gridLayout = new GridLayout(columns, true);
grid.setLayoutManager(gridLayout);

// Create a figure for an octant of the RodFigures.
for (int row = firstRow, i = 1; row < size; row++, i++) {
	// Add every RodFigure in this octant. Note the use of i.
	for (int col = firstRow; col < firstRow + i; col++)
		grid.add(rodGrid.get(row*size + col), new GridData(GridData.FILL_BOTH));
	
	// We need to insert placeholders to take up the empty space.
	if (i < columns) {
		Figure placeholder = new Figure();
		GridData filler = new GridData(GridData.FILL_BOTH);
		filler.horizontalSpan = columns - i;
		grid.add(placeholder, filler);
	}
}

// Add a figure that should be wrapped into an extra row. This worked for something similar recently.
Figure f = new Figure();
//f.setVisible(false);
f.setBorder(new LineBorder());
f.setPreferredSize(50, 50);
grid.add(f, new GridData(GridData.FILL_BOTH));

// Clear any gaps in the grid of figures.
gridLayout.horizontalSpacing = -1;
gridLayout.verticalSpacing = -1;

// Update the LightweightSystem's contents.
lws.setContents(grid);


I have also attached pictures of the UI before and after re-sizing the shell.

Any clue how to keep the last row the same size as the others? Also, are there better alternatives to using a GridLayout manager to lay out an evenly-spaced grid? I thought about using an XYLayout, but I'm being lazy here. Smile

Thanks!!!

Before resizing:
index.php/fa/15563/0/

After resizing:
index.php/fa/15564/0/
Re: draw2d GridLayout - different size of last row(s) [message #1067903 is a reply to message #1067869] Thu, 11 July 2013 08:06 Go to previous messageGo to next message
Jan Krakora is currently offline Jan KrakoraFriend
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
It's hard to say. You should provide us a SSCCE.
Meanwhile, I would recommend to you to debug Figure.setBounds() on the Figures in the last row.
Re: draw2d GridLayout - different size of last row(s) [message #1067966 is a reply to message #1067903] Thu, 11 July 2013 13:09 Go to previous message
Jordan Deyton is currently offline Jordan DeytonFriend
Messages: 10
Registered: July 2013
Junior Member
Jan Krakora wrote on Thu, 11 July 2013 04:06
It's hard to say. You should provide us a SSCCE.


Well, I just figured out a solution after slapping together a SSCCE as requested.

It turns out that with a GridLayout (in my case, where every cell has a constraint of GridData.FILL_BOTH), the last row and the last column absorb all of the spare space when the spare space is not quite enough to add to the rest of the cells (see my code example below).

For my octant grid (pictures in previous post), I believe the last row looks screwy because it is the only row with a visible cell in the last column, which was somehow messing with the sizes of the rest of the cells in the row. Adding an extra column to absorb the inadequate spare space seemed to fix the problem for me.

Here's the code example that helped me out. If you increase the size of the grid, it makes the last row/column sizing issue more noticeable.

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.GridData;
import org.eclipse.draw2d.GridLayout;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class GridTest {
	
	public static void main(String args[]) {

		// Shell stuff...
		Display display = new Display();
		Shell shell = new Shell(display);
		
		// canvas is all we've got in the shell.
		shell.setLayout(new FillLayout());
		
		// Create the Canvas, LightweightSystem, and set the size of the grid.
		Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED);
		LightweightSystem lws = new LightweightSystem(canvas);
		int size = 17;
		
		// Create the root figure and set its layout.
		Figure grid = new Figure();
		GridLayout layout = new GridLayout(size, false);
		grid.setLayoutManager(layout);
		grid.setBackgroundColor(ColorConstants.white);
		grid.setForegroundColor(ColorConstants.black);
		
		// Add the figures to the grid.
		// Change to i <= size*size to add in a figure wrapped to size-th row.
		for (int i = 0; i < size*size; i++) {
			Figure f = new Figure();
			f.setBorder(new LineBorder());
			f.setPreferredSize(50, 50);
			grid.add(f, new GridData(GridData.FILL_BOTH));
		}
		
		// Set the LightweightSystem's root figure.
		lws.setContents(grid);
		
		// Shell stuff...
		shell.setSize(1200, 900);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) display.sleep(); 
		} 
		display.dispose();
		
		return;
	}	
}


Still, if anyone has a recommendation for a more efficient and/or easier way to create grids of equally-sized Figures (no fixed locations/dimensions, please!), I'm all ears.

Thanks!
Previous Topic:Is there any editable text figure?
Next Topic:Orphan Command not executed
Goto Forum:
  


Current Time: Tue Mar 19 11:47:04 GMT 2024

Powered by FUDForum. Page generated in 0.02938 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top