Results 1 to 3 of 3
  1. #1
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default A Question about GridBagConstraints.fill

    Hi everyone,
    I have a problem with GridBagLayout, which is really confusing. It will be great if you can give me a hand

    Java Code:
    public class test  {
    	
    	
    	public static void main(String[] args){
    		
    		JFrame f=new JFrame();
    		JPanel p=(JPanel) f.getContentPane();
    		p.setLayout(new GridBagLayout());
    		p.setBackground(Color.CYAN);
    	
    		// Ja Button
    					JRadioButton ja = new JRadioButton("ja",false);
    					ja.setForeground(Color.WHITE);
    					ja.setFont(Global.font2);
    					ja.setBackground(Color.red);
    					ja.setIconTextGap(40);
    					GridBagConstraints c=new GridBagConstraints();
    					c.fill=GridBagConstraints.BOTH;
    					c.gridx=0;
    					c.gridy=0;
    					p.add(ja,c);
    					
    					
    					//Nein Button
    					JRadioButton nein = new JRadioButton("nein",false);
    					nein.setForeground(Color.WHITE);
    					nein.setFont(Global.font2);
    					nein.setBackground(Color.BLUE);
    				    nein.setIconTextGap(40);
    					c.fill=GridBagConstraints.BOTH;
    					c.gridx=0;
    					c.gridy=1;
    		
    		p.add(nein,c);
    		f.setSize(800,800);
    		f.setVisible(true);
    	}
    
    }
    I have used GridBagLayout for the root content panel, and added two JRadioButton to it, each of which are supposed to fill half of the panel. Why do they not fullfill
    c.fill=GridBagConstraints.BOTH;

    Thank you very much for your time :)

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

    Default Re: A Question about GridBagConstraints.fill

    You also need to set the weightx/y constraints, which tell the layout manager which components get allocated the extra space. The default is 0.

  3. #3
    tianchen is offline Member
    Join Date
    Jan 2014
    Posts
    34
    Rep Power
    0

    Default Re: A Question about GridBagConstraints.fill

    Thank you very much :) got it!

Similar Threads

  1. GridBagConstraints problem
    By nat in forum New To Java
    Replies: 0
    Last Post: 01-11-2014, 07:48 PM
  2. Positioning items with GridBagConstraints
    By jchinson in forum AWT / Swing
    Replies: 4
    Last Post: 07-04-2011, 11:17 PM
  3. GridBagConstraints help
    By noobgrammer in forum New To Java
    Replies: 5
    Last Post: 07-03-2010, 04:12 AM
  4. [SOLVED] [newbie] java.awt.GridBagConstraints
    By jon80 in forum New To Java
    Replies: 0
    Last Post: 05-27-2009, 03:40 PM
  5. GridBagConstraints
    By Aswq in forum New To Java
    Replies: 2
    Last Post: 07-21-2008, 09:08 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
  •