Results 1 to 3 of 3
  1. #1
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    102
    Rep Power
    0

    Default JButton positioning:

    Java Code:
    import java.awt.GridLayout;
    import javax.swing.*;
    
    public class Screen
    {
    	JButton start;
    	JButton reset;
    	JButton box[][] = new JButton[20][20];
    	
    	Screen()
    	{
    		JFrame j = new JFrame();
    		j.setSize(700,500);
    		j.setLayout(new GridLayout(20,20));
    		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//create buttons
    		for(int row=0;row<20;row++)
    		{
    			for(int col=0;col<20;col++)
    			{
    				box[row][col] = new JButton();
    				j.add(box[row][col]);
    			}
    		}
    		
    		//create start button
    		start = new JButton("Start");
    		j.add(start);
    		start.setLocation(0, 0);
    		
    		//create reset button
    		reset = new JButton("Reset");
    		j.add(reset);
    		reset.setLocation(2, 0);
    		
    		j.setVisible(true);
    		
    	}
    	
    	public static void main(String[] args)
    	{
    		new Screen();
    	}
    
    }
    I am trying to place the buttons on the bottom. I tried a few different things but the grid layout keeps grabbing it and making them a part of the grid at the button. Any advice?
    Last edited by jocdrew21; 05-21-2014 at 07:13 AM.

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: JButton positioning:

    Cross-post: http://www.javaprogrammingforums.com...sitioning.html

    You've set the JFrame to use GridLayout. When you "add" components to the JFrame, they are added based on the layout that is set in the JFrame.

    If you want to add the Start and Reset buttons outside the GridLayout, you'll need to nest your layout managers. E.g., you can:
    1. set JFrame to use a BorderLayout (JFrame has a BorderLayout by default)
    2. create a JPanel to use GridLayout, and add your "normal" buttons to this panel
    3. add this JPanel to the JFrame in the CENTER of the BorderLayout
    4. create another JPanel to use FlowLayout (i.e., JPanel's default layout manager)
    5. add the Start and Reset buttons to the second JPanel
    6. add the second JPanel to the JFrame in the SOUTH or PAGE_END of the BorderLayout
    Last edited by jashburn; 05-21-2014 at 06:35 PM. Reason: Add cross-post URL

  3. #3
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    102
    Rep Power
    0

    Default Re: JButton positioning:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    
    import javax.swing.*;
    
    public class Screen
    {
    	JButton start;
    	JButton reset;
    	JButton box[][] = new JButton[20][20];
    	
    	Screen()
    	{
    		
    		JFrame j = new JFrame("A* Algorithm");
    		JPanel p = new JPanel();
    		JPanel CmdBtns = new JPanel();
    		
    		j.setSize(1200,500);
    		j.setLayout(new BorderLayout());
    		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//p.setSize(15,30);
    		p.setLayout(new GridLayout(20,20));
    		
    		//create buttons
    		for(int row=0;row<20;row++)
    		{
    			for(int col=0;col<20;col++)
    			{
    				box[row][col] = new JButton();
    				p.add(box[row][col]);
    			}
    		}
    		
    		//create start button
    		start = new JButton("Start");
    		CmdBtns.add(start);
    		//start.setLocation(0, 0);
    		
    		//create reset button
    		reset = new JButton("Reset");
    		CmdBtns.add(reset);
    		//reset.setLocation(2, 0);
    		
    		//Centering matrix buttons onto main JFrame
    		j.add(p,BorderLayout.CENTER);
    		
    		//adding command buttons
    		j.add(CmdBtns,BorderLayout.SOUTH);
    		
    		j.setVisible(true);
    		
    	}
    	
    	public static void main(String[] args)
    	{
    		new Screen();
    	}
    
    }
    Thank you very much @jashburn.

Similar Threads

  1. AWT GUI Control positioning
    By Blondedude092 in forum New To Java
    Replies: 1
    Last Post: 10-22-2012, 08:44 PM
  2. Help in positioning and running to the web
    By jairoh_ in forum Java Applets
    Replies: 3
    Last Post: 10-10-2012, 08:36 PM
  3. Vertical box and positioning
    By aborgeld in forum New To Java
    Replies: 9
    Last Post: 04-01-2011, 08:33 PM
  4. Positioning using setBounds
    By thayalan in forum AWT / Swing
    Replies: 4
    Last Post: 04-28-2009, 02:59 AM
  5. panel positioning
    By shwein in forum New To Java
    Replies: 4
    Last Post: 09-09-2008, 06:15 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
  •