Results 1 to 10 of 10
  1. #1
    q8inq8 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default GridLayout alignment problem

    hello everyone,

    i`m new in java and it is the first homework,

    everything was done, unless the position of the graph.

    i use GridLayout N*N and as i said it work fine but there no space between the edges and first column, so how to add this space..?

    and how to make the last column have the same size between edges and and first column..?

    i hope somebody have solution of my above problem, because i was search in google for built in function that add space before first column and i fail.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    GridLayout allows you to specify horizontal and/or vertical gaps in the grid. Check out the GridLayout section of the API and it will show you how to do this using GridLayout's constructor.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Add a Border to the main panel?

  4. #4
    q8inq8 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    GridLayout allows you to specify horizontal and/or vertical gaps in the grid. Check out the GridLayout section of the API and it will show you how to do this using GridLayout's constructor.
    but when i add horizontal and vertical gap, first column is steel start from edges.

    Quote Originally Posted by camickr View Post
    Add a Border to the main panel?
    how to add border in main panel..?

    can you give an example, plz..

    --------------

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    
    public class hw01 extends Applet{
    
    	GridLayout tt = (new GridLayout(3, 3, 100, 0));
    	public void init(){
    		
    		setLayout(tt);
    	
    
    		add(new comp());
    		add(new comp());
    		add(new comp());
    
    		add(new comp());
    		add(new comp());
    		add(new comp());
    
    		add(new comp());
    		add(new comp());
    		add(new comp());
    		
    		
    	}
    
    }
    
    
    class comp extends Applet{
    	
    	public void paint(Graphics g){
    		setSize(this.getHeight(), this.getHeight());
    		int h = getSize().height-10, w = getSize().width-10,
    		x = 0, y = 0;
    		
    		
    		g.drawRect(0, 0, w, h);
    		for(int i = 0; i <= 10; i++){
    			
    			g.drawLine(0, h, w, y);
    			g.drawLine(0, h, x, 0);
    			g.drawLine(w, 0, 0, y);
    			g.drawLine(w, 0, x, h);
    			
    	
    			x += w/10;
    			y += h/10;
    			
    		}
    		
    	
    		
    	}
    
    }
    this is my program you can run it and see how is first column start fro, edges.. what i need is to add gap between first column and edges.. and add fixed gap between last column and other edges.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Border is a Swing concept, so I guess it won't help.

    I have no idea why schools still teach AWT. Everybody programs in Swing these days and that means you need to unlearn AWT concepts once you move on to Swing.

  6. #6
    q8inq8 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Border is a Swing concept, so I guess it won't help.

    I have no idea why schools still teach AWT. Everybody programs in Swing these days and that means you need to unlearn AWT concepts once you move on to Swing.

    thanks,

    anyone can help..plz!!

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Add Insets to the "outer" cells, or, as a small (unnecessary) trick, add a column and/or row to the sides and/or top/bottom with nothing but a simple label in it consisting of a single space (i.e. one label in the top left and one label in the bottom right).

  8. #8
    q8inq8 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Add Insets to the "outer" cells, or, as a small (unnecessary) trick, add a column and/or row to the sides and/or top/bottom with nothing but a simple label in it consisting of a single space (i.e. one label in the top left and one label in the bottom right).
    but what i should do in my homework is to have fixed gap between first column and edge, same thing for last column and other edge.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by q8inq8 View Post
    but what i should do in my homework is to have fixed gap between first column and edge, same thing for last column and other edge.
    Put your JPanel with the GridLayout in another JPanel with a BorderLayout (in the center) and put two invisible Boxes (read the API documentation) in the EAST and WEST parts. Note that nesting JPanels in JPanels with different LayoutManagers can do quite a powerful job.

    kind regards,

    Jos

  10. #10
    q8inq8 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Put your JPanel with the GridLayout in another JPanel with a BorderLayout (in the center) and put two invisible Boxes (read the API documentation) in the EAST and WEST parts. Note that nesting JPanels in JPanels with different LayoutManagers can do quite a powerful job.

    kind regards,

    Jos
    that what i try to do now, but there are no output..!!

    what i do is add this code to the above code in my previous code
    Java Code:
    public class hw01 extends Applet{
    	public void init(){                                              
    	    setLayout (new BorderLayout());
    	    
    	    add(new comp2(), BorderLayout.CENTER);
    	  }
    	
    }

    is it true or should i do it in another way..?

Similar Threads

  1. SWT GridLayout Demo
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:48 PM
  2. [SOLVED] alignment problem
    By nanimtech in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-10-2008, 02:23 PM
  3. SWT GridLayout
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 10:04 AM
  4. GridLayout with animation?
    By tojas in forum AWT / Swing
    Replies: 3
    Last Post: 11-12-2007, 11:16 PM
  5. Gridlayout
    By Marty in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 12:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •