Results 1 to 1 of 1
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    122
    Rep Power
    0

    Default GridBagLayout: Unexpected output and some questions

    I am following a tutorial to understand the GridBagLayout and the code I wrote (which is almost similar to the one used to teach) gives an unexpected result. Following is my code followed by screenshot of what turned out, followed by the code used in the tutorial...

    OK, so my questions are:

    1. Firstly I made a new GridBagConstraints object for each component because it is written in this tutorial, "However, it is recommended that you do not reuse GridBagConstraints, as this can very easily lead to you introducing subtle bugs if you forget to reset the fields for each new instance." So I was trying to practice the good thing. Is that inefficient?


    1. Secondly, I did set the gridx and gridy properties to the first column and the second row. But it is still in the first row (and second column), although from the other values and from the example I have gathered that gridx represents columns and gridy represents rows. So where am I wrong here?


    1. Thirdly, can you tell me what weight property (weightx and weighty) is in the simplest possible way, may be some example? In Android I had read that the weight property is something like if one widget has it set to two and the other one has a weight of one, then the first one takes double the space taken by the second one. That was very understandable. Is it something like that too?

      When he had set the weight of the button to 10 (that for the other components is 0.5) the button had a big area around it vertically, but after setting the anchor to FIRST_LINE_START, it just went near the other components, why is that?


    1. Moreover, the preferred size of the panel is 350 pixels in my program, while that of the parent frame is 500 pixels. So the panel covers half of the frame when the frame is in this size, but when I expand (maximize) the window, the panel remains the same size.
      What I want is that the aspect ratios of the components sizes to the containers sizes should be maintained, when the window is expanded. How can I ensure that? Are there any methods?


    Java Code:
    public class DetailsPanel extends JPanel {
    	
    	DetailsPanel(){
    		Dimension size= getPreferredSize();
    		size.width=350;
    		setPreferredSize(size);
    		
    		setBorder(BorderFactory.createTitledBorder("Personal Details"));
    		
    		JLabel nameLabel= new JLabel("Name ");
    		JLabel occupationLabel= new JLabel("Occupation ");
    		JTextField nameField = new JTextField(10);
    		JTextField occupationField = new JTextField(10);
    		JButton addButton = new JButton("Add");
    		
    		setLayout(new GridBagLayout());
    		
    		
    		//1st column
    		GridBagConstraints c1= new GridBagConstraints();
    		c1.gridx=0;
    		c1.gridy=0;
    		c1.weightx=0.5;
    		c1.weighty=0.5;
    		c1.anchor=GridBagConstraints.LINE_END;
    		add(nameLabel, c1);
    		
    		GridBagConstraints c2= new GridBagConstraints();
    		c2.gridx=0;
    		c2.gridy=1;
    		c1.weightx=0.5;
    		c1.weighty=0.5;
    		c2.anchor=GridBagConstraints.LINE_END;
    		add(occupationLabel);
    		
    		//second column
    		GridBagConstraints c3= new GridBagConstraints();
    		c3.gridx=1;
    		c3.gridy=0;
    		c3.weightx=0.65;
    		c3.weighty=0.5;
    		c3.anchor=GridBagConstraints.LINE_START;
    		add(nameField, c3);
    		
    		GridBagConstraints c4= new GridBagConstraints();
    		c4.gridx=1;
    		c4.gridy=1;
    		c4.weightx=0.65;
    		c4.weighty=0.5;
    		c4.anchor=GridBagConstraints.LINE_START;
    		add(occupationField, c4);
    		
    		//final row
    		GridBagConstraints c5= new GridBagConstraints();
    		c5.gridx=1;
    		c5.gridy=2;
    		c5.weightx=0;
    		c5.weighty=10;
    		c5.anchor=GridBagConstraints.FIRST_LINE_START;
    		add(addButton, c5); 
    }}
    GridBagLayout: Unexpected output and some questions-14.png

    Java Code:
    public class DetailsPanel extends JPanel {
    	
    	DetailsPanel(){
    		Dimension size= getPreferredSize();
    		size.width=350;
    		setPreferredSize(size);
    		
    		setBorder(BorderFactory.createTitledBorder("Personal Details"));
    		
    		JLabel nameLabel= new JLabel("Name ");
    		JLabel occupationLabel= new JLabel("Occupation ");
    		JTextField nameField = new JTextField(10);
    		JTextField occupationField = new JTextField(10);
    		JButton addButton = new JButton("Add");
    		
    		setLayout(new GridBagLayout());
    	
    		GridBagConstraints gc= new GridBagConstraints();
    		
    		gc.anchor = GridBagConstraints.LINE_END;
            gc.weightx = 0.5;
            gc.weighty = 0.5;
             
            gc.gridx = 0;
            gc.gridy = 0;
            add(nameLabel, gc);
             
            gc.gridx = 0;
            gc.gridy = 1;
            add(occupationLabel, gc);
             
            //// Second column
            gc.anchor = GridBagConstraints.LINE_START;
             
            gc.gridx = 1;
            gc.gridy = 0;
            add(nameField, gc);
             
            gc.gridx = 1;
            gc.gridy = 1;
            add(occupationField, gc);
             
            // Final row
            gc.weighty = 10;
             
            gc.anchor = GridBagConstraints.FIRST_LINE_START;
            gc.gridx = 1;
            gc.gridy = 2;
            add(addButton, gc);
            
    	}
    
    }
    GridBagLayout: Unexpected output and some questions-13.png


    Thank you very much!
    Last edited by Zarah; 05-02-2014 at 02:37 PM.

Similar Threads

  1. Creating a thread. Unexpected output.
    By Zarah in forum New To Java
    Replies: 7
    Last Post: 03-19-2014, 09:59 AM
  2. Unexpected Output… Please help
    By Nazneen Ali in forum New To Java
    Replies: 7
    Last Post: 11-23-2013, 08:03 PM
  3. Unexpected Output
    By ankiit in forum New To Java
    Replies: 20
    Last Post: 01-06-2012, 06:48 PM
  4. A basic method with an unexpected output
    By JONCOM in forum New To Java
    Replies: 6
    Last Post: 02-01-2011, 08:25 AM
  5. GridBagLayout problems and questions
    By g123456 in forum New To Java
    Replies: 12
    Last Post: 06-02-2010, 03:25 AM

Posting Permissions

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