By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 820 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

GridBag Layout Size Issues

P: n/a
I have read previous posts on the gridbaglayout and none seem to answer my
questions (sorry if any of these have been aske dI really did search and
found one large thread which took me an hour to read..
Anyway my problem is as such i need to make something like a simcity game
(not quite though). Bassically I need a grid in which labels (using the
image icon command) can be disbladed. This grid needs to be resizable ie it
could go from 20x20 to 20x25 or something like that. I currently have the
grid set inside a JPanel which is set inside a Tab.... My problem is that
when I place stuff in the grid the settings are all messed up. Nothing
spaces out correctly and its really messy and ugly. Below is a diagram of
what im looking for

/---------------------\/-------------------------\
| Tab 1 | Tab 2 |
\---------------------|--------------------------/
/Tab Area----------------------------------\
| ------- -------- ------- -------- ------- |
| |Label| | | | | | | | | |
| ------- -------- ------- -------- ------- |
| ------- -------- ------- -------- ------- |
| | | | | | | | | | | |
| ------- -------- ------- -------- ------- |
| ------- -------- ------- -------- ------- |
| | | | | | | | | | | |
| ------- -------- ------- -------- ------- |
\--------------------------------------------/
The above is an example of a 5 by 3 grid Eahc area is a labwel which will
contain an icon being exactly 48by48pixels.
When the grid is say smaller (say 1x1) i want it to be centered (ie the
label sizes remains the same) and the grid area is just cenetered IE Below:

/---------------------\/-------------------------\
| Tab 1 | Tab 2 |
\---------------------|--------------------------/
/Tab Area----------------------------------\
| |
| |
| |
| ------- |
| | | |
| ------- |
| |
| |
\--------------------------------------------/

My problem is I get this when i try a 5 by 5 grid
/---------------------\/-------------------------\
| Tab 1 | Tab 2 |
\---------------------|--------------------------/
/Tab Area----------------------------------\
| |
| |
| ------ |
| |------| |
| -| | |
| ------- |
| |
| |
\--------------------------------------------/

IE the grids dont space out and they overlap each other..and I cannot for
the life of me figure out why.!! The code is below any suggestions?

private LandscapeViewControl theModel;
private GridSection[][] gridLayout;

// The current layer which is being built (ie base, plant, canopy)
private int theLayer;

// Layout Properties
private GridBagConstraints constraints;
private GridBagLayout gbLayout;

public GridLayers(LandscapeViewControl model, int layer)
{
this.theModel = model;
this.theLayer = layer;
this.theModel.addView(this);

this.makeGrid();
JLabel xLabel = new JLabel(" dfs", SwingConstants.CENTER);

this.addComponent(xLabel,2,2);
JLabel yLabel = new JLabel(" dfs2", SwingConstants.CENTER);
this.addComponent(yLabel, 1,1);
//this.registerListeners();
}

private void makeGrid()
{
int height = this.theModel.getGridHeight();
int width = this.theModel.getGridWidth();
// Creates an array of GridSections in the gridlayout variable
this.gridLayout = new GridSection[width][height];

// Sets the layout manager to be the 'GridLayout' with 1 extra row and
column
// for numbers to help the user find the correct square on the grid
gbLayout = new GridBagLayout();
constraints = new GridBagConstraints();
this.setLayout(gbLayout);

// Fills the gird with information
//this.fillGrid(width,height);
}

private void addComponent(Component component, int row, int column)
{
// set the gridx and gridy
constraints.gridx = column;
constraints.gridy = row;

// Set the gridwidth and gridheight
constraints.gridwidth = 48;
constraints.gridheight = 48;
gbLayout.setConstraints(component, constraints);
this.add(component);
}

Bassically where the
this.addComponent(xLabel,2,2);
JLabel yLabel = new JLabel(" dfs2", SwingConstants.CENTER);
this.addComponent(yLabel, 1,1);
//this.registerListeners();
is located a a call to the function below will be called (the labels are in
simply for testing purposes) So i m guessing if it can startt working wiuth
the above code then the below code will work to.. Any possiblities of help?
or adivce?

private void fillGrid(int width, int height)
{
// Function lists labels straight across (1 to the width of the grid
or the number of columns)
this.createXLabels(width);

// Loops through as many times as the user specificed
for (int y = 0; y < height; y++)
{
// Sames as the Createxlabel except is labels vertically
this.createYLabel(y);

// Loops through and adds the labels for this particular row
for (int x = 0; x < width; x++)
{
GridSection square = new GridSection(width, height, theLayer,
theModel);
this.gridLayout[x][y] = square;
//this.add(square);
constraints.fill = GridBagConstraints.BOTH;
this.addComponent(square, y, x);
} // for
} // for
} // fillGrid
THE GRIDSECTION CLASS in case it helps
public class GridSection extends JLabel
{
// Constants which determine thw width and height of each sqaure
public static final int SQUARE_W = 48;
public static final int SQUARE_H = 48;

private int width;
private int height;
private int theLayer;
private LandscapeViewControl theModel;

public GridSection(int X, int Y, int layer, LandscapeViewControl model)
{
this.theModel = model;
this.width = X;
this.height = Y;
this.theLayer = layer;

this.setSection();
}

private void setSection()
{
// Sets the size of the square
//this.setSize(new Dimension(SQUARE_W, SQUARE_H));
//this.setPreferredSize(new Dimension(SQUARE_W, SQUARE_H));

// Sets the square border to be raised
this.setBorder(new EtchedBorder(EtchedBorder.RAISED));
this.setIcon(new ImageIcon(this.theModel.getImage(width, height,
theLayer)));
}

public void updateData()
{
// Set the label to the new image icon for the corresponding section
this.setIcon(new ImageIcon(this.theModel.getImage(width,height,
theLayer)));
}
}
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.