473,385 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

GridBag Layout Size Issues

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
0 1937

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
49
by: lime | last post by:
I have addressed most of the issues listed in the responses to my last post "Critique CSS layout (1st go - fingers crossed)". I'm rapt with the progress I have made, thanks to all for your past...
8
by: kaeli | last post by:
I have had a little free time lately to revisit a problem I have with the 3 column layout plus a header and footer. See this example: http://glish.com/css/7.asp There is a header and 3...
0
by: Old Wolf | last post by:
I have a site that I laid out with tables, and lots of grotty hacks and lots of markup generated by client-side javascript. I'm in the process of converting it to "nice" CSS code but have got...
4
by: NWx | last post by:
Hi, I develop an ASP.NET app which should be used from Internet, so I don't have control over what browsers will be used. I don't want to target every possible users, so I don't really mind is...
11
by: Henryk | last post by:
I have something like class Params { public: const static char nOne = 1; const static int nTwo = 2; const static char nThree = 3; }; This is just a wrapper for globally used parameters in...
5
by: dmorand | last post by:
I'm having an issue with my layout, and am looking for some advice. The layout looks fine until the user changes the font size in the browser. Then my footer and navigation bar push down to another...
9
by: Eric Lindsay | last post by:
How do you provide a consistent gradient fill as a background in a liquid layout? If I make a gradient fill image say 1000 pixels wide (and repeat it down the page) to suit a typical computer...
3
by: gaz123 | last post by:
I realise this is probably something simple, but here goes. I'm a home-taught student and have been set the task of creating a GUI and engine for a game. My engine works fine, as did my GUI before my...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.