4 Replies - 337 Views - Last Post: 18 February 2012 - 09:21 PM Rate Topic: -----

#1 greenpool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 27-November 11

GUI and gridBagLayout question

Posted 18 February 2012 - 06:14 PM

Hi there,

am trying to create a GUI to look like the one in the attachment.

now i'm having trouble with the last component.. : /

as you can see in the attachment before adding the button the components align but when i add the button i'm not really sure how to move it to the left to align it with the label on top (ref to point 2 in image)


i'm also not sure why it creates a gap between the labTotal label and labShowTotal label (ref to point 1 in image)


Code snippet:


//Create panel with gridBagLayout
		JPanel panel = new JPanel(new GridBagLayout());
	
		
		GridBagConstraints c = new GridBagConstraints();
		
		
		
		/*Initializing components*/
		
		labCartons= new JLabel("Cartons per shipment: ");
		labItems = new JLabel("Items per carton: ");
		labtotal = new JLabel("Total: ");
		labShowTotal = new JLabel("           ");
		
		
		

	    Border border = LineBorder.createGrayLineBorder();
		labShowTotal.setBorder(border);
		
		
		txtCartons = new JTextField(3);
		txtItems  = new JTextField(3);
		
		btnTotal = new JButton("Calculate total");
		
		
		
		/*Adding components to the grid*/
		
		c.fill = GridBagConstraints.HORIZONTAL;
		
		//add cartons label
		c.gridx=0;
		c.gridy=0;
		panel.add(labCartons,c);
		
		
		//add cartons textfield
		c.gridx=1;
		c.gridy=0;
		c.insets=new Insets(0,0,0,10);
		
		panel.add(txtCartons,c);
		
		
		//add total label
		c.gridx=2;
		c.gridy=0;
		c.insets=new Insets(0,0,0,0);
		panel.add(labtotal,c);
		
		
		//add showTotal label
		c.gridx=3;
		c.gridy=0;
		
		
		panel.add(labShowTotal,c);
		
		
		//add items label
		
		
		c.gridx=0;
		c.gridy=1;
		
		panel.add(labItems,c);
		
		
		//add items textfield
		c.gridx=1;
		c.gridy=1;
		c.insets=new Insets(0,0,0,10);
		
		panel.add(txtItems,c);
		
		
		//add calculate total button
		
		c.gridx=2;
		c.gridy=1;
		
		panel.add(btnTotal, c);
		





One more question :)

how do you assign the size of a label to be the size of a textfield?

i was wondering how to get the labshowTotal label to be the same size as labCartons or labItems label.

currently i'm using spaces when i initalize it and i'm sure there is a better way of doing it.

thanks for any suggestion/advice!

Attached image(s)

  • Attached Image
  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: GUI and gridBagLayout question

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: GUI and gridBagLayout question

Posted 18 February 2012 - 06:24 PM

You can make the button span 2 columns thus it will be under both the label and the textbox in a couple of different ways:
//option 1
c.gridwidth = GridBagConstraints.REMAINDER; //fills remaining space in this case 2 columns

//option 2
c.weightx = 2; //sets the weight of items, this also helps determine growth when panels are stretched


All items should be given weights. Even if you set them all to 1 aside from this button.

1 letter variable names are bad practice. The only case I can think of where they are acceptable is an index variable in for loops.
Was This Post Helpful? 1
  • +
  • -

#3 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: GUI and gridBagLayout question

Posted 18 February 2012 - 06:25 PM

The image you showed looks more like a GridLayout then a GridBagLayout to me :) . I guess you could accomplish a GridLayout with a GridBag though...

This post has been edited by ianian112: 18 February 2012 - 06:26 PM

Was This Post Helpful? 0
  • +
  • -

#4 greenpool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 27-November 11

Re: GUI and gridBagLayout question

Posted 18 February 2012 - 06:29 PM

View Postianian112, on 18 February 2012 - 06:25 PM, said:

The image you showed looks more like a GridLayout then a GridBagLayout to me :) . I guess you could accomplish a GridLayout with a GridBag though...


Interesting you say that cos i tried gridLayout but then i couldn't figure out how to expand the button along 2 columns so resorted to gridBagLayout.

in the interest of learning, is there a way to span a component across two or more columns when using gridLayout? :)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: GUI and gridBagLayout question

Posted 18 February 2012 - 09:21 PM

Avoid GridBagLayout... leave it to GUI generators like NetBeans
there are more intelligent, and effort less, way of achieving the same goal
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1