tags:

views:

310

answers:

2

When i am declaring rows and columns to my gridlayout.eg: 3x2 (3 rows and 2 cols).I have to get a skeletal view of these rows and columns.Like a dotted rectangles.How can we achieve this by using swing ?

regards Mathan

A: 

Add a Border to each of your components in the gridlayout.

Pierre
A: 

Subclass a JPanel to draw a grid within paintChildren(). Any components you add to this panel will show this grid. For example:

public class DebugPanel extends JPanel {

    @Override
    protected void paintChildren(Graphics g) {
     super.paintChildren(g);
     g.setColor(Color.GRAY);
     // dashed stroke
     BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 1,new float[]{5},0);
     ((Graphics2D)g).setStroke(stroke);  
     drawComponentGrid(this,g);
    }

    /**
     * Drawn vertical and horizontal guides for a containers child 
     * components
     */
    private static void drawComponentGrid(Container c, Graphics g) {
     for(int i=0;i<c.getComponentCount();++i) {
      Rectangle r = c.getComponent(i).getBounds();
      int x = r.x;
      // vertical line at left edge of component
      g.drawLine(x,0,x,c.getHeight());
      x+= r.width;
      // vertical line at right edge of component
      g.drawLine(x,0,x,c.getHeight());

      // horizontal line at top of component
      int y = r.y;
      g.drawLine(0,y,c.getWidth(),y);
      // horizontal line at bottom of component
      y+= r.height;
      g.drawLine(0,y,c.getWidth(),y);
     }
    }
}
Joel Carranza