Results 1 to 2 of 2
  1. #1
    orushw is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default Problem with GridBagLayout.

    Hi,
    I'm using the GridBagLayout manager and i want to add my first button at the left corener of my frame (0,0).
    The button locate him self to the center of my frame, and i dont know how to move it to the left corener.
    This is what i want to do :
    Problem with GridBagLayout.-button.png
    I want to locate the button at the red place.
    This is my code :
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    
    public class Display {
    	
    	private JFrame frame;
    	private JPanel panel;
    	private JList<String> list;
    	
    	public Display()
    	{
    		frame = new JFrame();
    		panel = new JPanel(new GridBagLayout());
    		list = new JList<String>();
    		
    		frame.setVisible(true);
    		frame.setSize(600,600);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		GridBagConstraints grid = new GridBagConstraints();
    		//Set the spaces between the components and order them VERTICAL.
    		grid.fill = GridBagConstraints.VERTICAL;
    		grid.insets = new Insets(15,15,15,15); 
    		
    		int day_of_week = 7;
    		for(int i=0;i<28;i++)
    		{
    			grid.gridx = i % 7;
    			grid.gridy = i / 7 ;
    			list = new JList<String>();
    			panel.add(list,grid);
    		}
    		frame.add(panel);
    	}
    }
    How do i use the GridBagLayout to locate the button there?
    Thanks alot,
    Or.

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

    Default Re: Problem with GridBagLayout.

    You're not setting all the pertinent fields of the GridBagConstraints object. Look at the API and the tutorial. For example you'll want to set the weight constraints among others.

Similar Threads

  1. GridBagLayout Component Problem...
    By danpotter in forum AWT / Swing
    Replies: 5
    Last Post: 05-09-2012, 11:34 PM
  2. GridBagLayout alignment problem
    By adwart in forum AWT / Swing
    Replies: 2
    Last Post: 08-15-2011, 04:58 AM
  3. GridBagLayout problem!
    By canersen in forum AWT / Swing
    Replies: 9
    Last Post: 03-06-2011, 11:35 PM
  4. Problem with GridBagLayout
    By ze snow in forum New To Java
    Replies: 20
    Last Post: 02-28-2010, 06:08 AM
  5. Problem with GridBagLayout
    By Daniel in forum SWT / JFace
    Replies: 2
    Last Post: 07-01-2007, 06:57 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
  •