Viewing GridBagLayout's Grid Lines?

J

John Davison

It would be very helpful when doing layouts with GridBagLayout if I
could see the grid lines. Is there a way to make the grid lines
visible?

-- John
 
C

Chris Smith

John said:
It would be very helpful when doing layouts with GridBagLayout if I
could see the grid lines. Is there a way to make the grid lines
visible?

One trick I have used in the past, especially when teaching, is to set
controls to different background colors. That makes the exact
boundaries between controls on the screen precisely visible. Not
exactly what you wanted, but it might help...

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

Sandip Chitale

One technique may be:

For each of your component added to the GridBagLayout

Modify its contraint fill=BOTH
Replace it with a JLabel with a line border.
The JLabels label could show the name of original component.

Just a thought.
 
K

Karsten Lentzsch

John said:
It would be very helpful when doing layouts with GridBagLayout
if I could see the grid lines. Is there a way to make the grid lines
visible?

The JGoodies Forms layout system ships with a debug panel
that provides different options to paint the FormLayout grid,
see http://www.jgoodies.com/freeware/formsdemo/index.html

The code is open source and it should be no big deal
to translate it to the GridBagLayout. Both layout managers
use a LayoutInfo object that comprises the actual grid data.

Forms ships with a second debug class, FormDebugUtils,
that I find useful too. It dumps a lot of information about
the layout manager state, its grid, the components and
their associated constraints. This class could be converted
to GridBagLayout too.

You can download the Forms distribution including sources
from JavaDesktop.org where the project is maintained, see
the file sharing section at http://forms.dev.java.net/
If you convert these classes to GridBagLayout I should
appreciate if you would send me the code, so I can include
it with the Forms distribution.

Also, you may consider given FormLayout a try. It has been
designed to overcome shortcomings in the GridBagLayout.

Best regards,
 
T

Todd Corley

If you consolidate your layout code into a method, you can do some
neat things like the following that adds a titled border to all added
comps that includes the general GBC info. The titled border will add
a line around the components as well, showing the boundries of
everything.



public static Component set(Container cont, JComponent comp,
int x, int y, int w, int h,
double wx, double wy,
int top, int left, int bottom, int
right,
int fill, int anchor)
{
GridBagLayout gbl = getLayout( cont );
clearGridBagConstraints();
gbc.fill = fill;
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
gbc.weightx = wx;
gbc.weighty = wy;
gbc.insets = setInsets(ins, top, left, bottom, right);
gbc.anchor = anchor;
comp.setBorder( BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(
"x="+x+
" y="+y+
" w="+w+
" h="+h ) ),
comp.getBorder() ) );

cont.add(comp, gbc);
return comp;
}
 
T

Tor Iver Wilhelmsen

It would be very helpful when doing layouts with GridBagLayout if I
could see the grid lines. Is there a way to make the grid lines
visible?

Some bean tools/GUI designers do - like JBuilder from Borland.
 
T

Todd Corley

If you consolidate your layout code into a method, you can do some
neat things like the following that adds a titled border to all added
comps that includes the general GBC info. The titled border will add
a line around the components as well, showing the boundries of
everything.



public static Component set(Container cont, JComponent comp,
int x, int y, int w, int h,
double wx, double wy,
int top, int left, int bottom, int
right,
int fill, int anchor)
{
GridBagLayout gbl = getLayout( cont );
clearGridBagConstraints();
gbc.fill = fill;
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
gbc.weightx = wx;
gbc.weighty = wy;
gbc.insets = setInsets(ins, top, left, bottom, right);
gbc.anchor = anchor;
comp.setBorder( BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(
"x="+x+
" y="+y+
" w="+w+
" h="+h ) ),
comp.getBorder() ) );

cont.add(comp, gbc);
return comp;
}

Woops, I copied this from some of my existing code and left some stuff
dangeling.
gbc is a static GridBagConstraints.
getBorder( Component comp ) is another static method that gets the
GridBagLayout from the component or does some additional setup if the
current layout is not a GBL.

Hope this helps,
Todd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top