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)
//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!