Ok following the suggestion from another user I have switched over to a GroupBagLayout. Following the java tutorial in hindsight this seems easier. But alas, I have ran into a problem which does not make any sense to me.

See screenshot:

http://img189.imageshack.us/img189/1060/groupbaglayout.png

I have started from a top down approach to make my life easier...What I don't get is why is the grid starting in the middle of the frame???

Code:

//Create Panels and TextBoxes
	private Container createContentPane()
	{
		//Creating main JPanel and setting the layout manager...
		JPanel contentPane = new JPanel();
		contentPane.setLayout(new GridBagLayout());
		GridBagConstraints gridC = new GridBagConstraints();
		contentPane.setOpaque(true);
		
		if(shouldFill)
		{
			//natural Height/width
			gridC.fill = GridBagConstraints.HORIZONTAL;
		}
		
		//Creating the Incoming data, JTextArea
		incDisp = new JTextArea(5, 30);
		incDisp.setEditable(false);
		incDisp.setLineWrap(true);
		incDisp.setWrapStyleWord(true);
		JScrollPane incScrollPane = new JScrollPane(incDisp);
		
		//Adding incScrollPane to GridBagLayout
		if (shouldWeightX) 
		{
			   gridC.weightx = 0.5;
		}
		
		gridC.fill = GridBagConstraints.HORIZONTAL;
		//gridC.anchor = GridBagConstraints.CENTER;
		//gridC.weightx = 0.5;
		//gridC.weighty = 1.0;
		gridC.gridx = 0;
		gridC.gridy = 0;
		gridC.gridwidth = 2;
		gridC.gridheight = 2;
		contentPane.add(incScrollPane, gridC);
		
		//Create JLabel for usrDisp and Adding it to GridBagLayout
		JLabel usrLabel = new JLabel("Contacts");
		gridC.fill = GridBagConstraints.HORIZONTAL;
		gridC.anchor = GridBagConstraints.FIRST_LINE_END;
		//gridC.weightx = 0.5;
		//gridC.weighty = 0.5;
		gridC.gridx = 2;
		gridC.gridy = 0;
		contentPane.add(usrLabel, gridC);
		
		//Creating the user list, JTextArea
		/*usrDisp = new JTextArea(0, 0);
		usrDisp.setEditable(false);
		usrDisp.setLineWrap(true);
		usrDisp.setWrapStyleWord(true);
		JScrollPane usrScrollPane = new JScrollPane(usrDisp);
		gridC.fill = GridBagConstraints.VERTICAL;
		gridC.anchor = GridBagConstraints.LINE_END;
		//gridC.weightx = 1.0;
		//gridC.weighty = 1.0;
		gridC.gridx = 3;
		gridC.gridy = 1;
		gridC.gridwidth = 0;
		gridC.gridheight = 2;
		contentPane.add(usrScrollPane, gridC);*/
		
		//Creating the send data, JTextArea
		/*senDisp = new JTextArea(4, 30);
		senDisp.setEditable(true);
		senDisp.setLineWrap(true);
		senDisp.setWrapStyleWord(true);
		JScrollPane senScrollPane = new JScrollPane(senDisp);
		gridC.fill = GridBagConstraints.HORIZONTAL;
		gridC.anchor = GridBagConstraints.PAGE_END;
		//gridC.weightx = 1.0;
		//gridC.weighty = 1.0;
		gridC.gridx = 0;
		gridC.gridy = 2;
		gridC.gridwidth = 3;
		contentPane.add(senScrollPane, gridC);*/
		
		//Setting up the layout manager and placing items
		/*contentPane.add(incScrollPane, BorderLayout.CENTER);
		contentPane.add(senScrollPane, BorderLayout.SOUTH);
		contentPane.add(usrScrollPane, BorderLayout.EAST);
		contentPane.add(usrLabel);*/
		
		
		return contentPane;
	}

This is what is should look like when it is completed:
http://img268.imageshack.us/img268/2757/grouplayout.png

Recommended Answers

All 5 Replies

You have used only weightx and not weighty and you have only used a horizontal fill, so your elements will not even attempt to fill the space vertically, and all "unused" space in the container will dispersed evenly around the components (i.e. the "free" vertical space will be half above and half below the components and the "free" horizontal space will be half to the left and half to the right of the components).

Study this

package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class BogusGridTest {

	private static void doIt() {
		JFrame.setDefaultLookAndFeelDecorated(false);

		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JMenuBar mb = new JMenuBar();
		mb.add(new JMenu("File"));
		frame.setJMenuBar(mb);

		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		frame.getContentPane().setLayout(gbl);

		gbl.columnWeights = new double[] { 0.7, 0.3 };
		gbl.rowWeights = new double[] { 0.1, 0.7, 0.2 };

		gbc.anchor = GridBagConstraints.CENTER;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		gbc.insets = new Insets(1, 1, 1, 1);
		gbc.ipadx = 0;
		gbc.ipady = 0;
		gbc.weightx = 0.0;
		gbc.weighty = 0.0;

		JPanel incScrollPane = new JPanel();
		incScrollPane.setBackground(Color.WHITE);
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridheight = 2;
		gbl.setConstraints(incScrollPane, gbc);
		frame.getContentPane().add(incScrollPane);

		JPanel usrLabel = new JPanel();
		usrLabel.setBackground(Color.GREEN);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridheight = 1;
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbl.setConstraints(usrLabel, gbc);
		frame.getContentPane().add(usrLabel);

		JPanel usrScrollPane = new JPanel();
		usrScrollPane.setBackground(Color.YELLOW);
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbl.setConstraints(usrScrollPane, gbc);
		frame.getContentPane().add(usrScrollPane);

		JPanel senScrollPane = new JPanel();
		senScrollPane.setBackground(Color.CYAN);
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.gridheight = GridBagConstraints.REMAINDER;
		gbl.setConstraints(senScrollPane, gbc);
		frame.getContentPane().add(senScrollPane);

		frame.setSize(400, 200);
		frame.setPreferredSize(new Dimension(400,200));
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(
				new Runnable() {
					public void run() {
						doIt();
					}
				}
			);
	}
}

Ok after pondering on what you said and reading the code I have made progress but stuck on the JLabel and the usr JTextArea.

http://img186.imageshack.us/img186/1060/groupbaglayout.png

As you can see from the screenshot the contacts JLabel is on top of the usr text area and the text area doesn't keep its x size when resized.

CODE:

//Create Panels and TextBoxes
	private Container createContentPane()
	{
		//Creating main JPanel and setting the layout manager...
		JPanel contentPane = new JPanel();
		contentPane.setLayout(new GridBagLayout());
		GridBagConstraints gridC = new GridBagConstraints();
		contentPane.setOpaque(true);
		
		if(shouldFill)
		{
			//natural Height/width
			gridC.fill = GridBagConstraints.HORIZONTAL;
		}
		
		//Creating the Incoming data, JTextArea
		incDisp = new JTextArea(5, 30);
		incDisp.setEditable(false);
		incDisp.setLineWrap(true);
		incDisp.setWrapStyleWord(true);
		JScrollPane incScrollPane = new JScrollPane(incDisp);
		
		//Adding incScrollPane to GridBagLayout
		if (shouldWeightX) 
		{
			   gridC.weightx = 0.5;
		}
		
		gridC.fill = GridBagConstraints.BOTH;
		gridC.anchor = GridBagConstraints.CENTER;
		gridC.weightx = 1.0;
		gridC.weighty = 1.0;
		gridC.gridx = 0;
		gridC.gridy = 0;
		gridC.gridwidth = 2;
		gridC.gridheight = 2;
		contentPane.add(incScrollPane, gridC);
		
		//Create JLabel for usrDisp and Adding it to GridBagLayout
		JLabel usrLabel = new JLabel("Contacts");
		gridC.fill = GridBagConstraints.HORIZONTAL;
		gridC.anchor = GridBagConstraints.FIRST_LINE_END;
		gridC.weightx = 0.0;
		gridC.weighty = 0.0;
		gridC.gridx = 2;
		gridC.gridy = 0;
		gridC.gridwidth = 0;
		gridC.gridheight = 0;
		contentPane.add(usrLabel, gridC);
		
		//Creating the user list, JTextArea
		usrDisp = new JTextArea(0, 0);
		usrDisp.setEditable(false);
		usrDisp.setLineWrap(true);
		usrDisp.setWrapStyleWord(true);
		JScrollPane usrScrollPane = new JScrollPane(usrDisp);
		gridC.fill = GridBagConstraints.BOTH;
		gridC.anchor = GridBagConstraints.LINE_END;
		gridC.weightx = 0.0;
		gridC.weighty = 1.0;
		gridC.gridx = 3;
		gridC.gridy = 1;
		gridC.gridwidth = 0;
		gridC.gridheight = 2;
		contentPane.add(usrScrollPane, gridC);
		
		//Creating the send data, JTextArea
		senDisp = new JTextArea(4, 30);
		senDisp.setEditable(true);
		senDisp.setLineWrap(true);
		senDisp.setWrapStyleWord(true);
		JScrollPane senScrollPane = new JScrollPane(senDisp);
		gridC.fill = GridBagConstraints.BOTH;
		gridC.anchor = GridBagConstraints.PAGE_END;
		gridC.weightx = 1.0;
		gridC.weighty = 0.1;
		gridC.gridx = 0;
		gridC.gridy = 2;
		gridC.gridwidth = 4;
		//gridC.gridheight = 0;
		contentPane.add(senScrollPane, gridC);
		
		//Setting up the layout manager and placing items
		/*contentPane.add(incScrollPane, BorderLayout.CENTER);
		contentPane.add(senScrollPane, BorderLayout.SOUTH);
		contentPane.add(usrScrollPane, BorderLayout.EAST);
		contentPane.add(usrLabel);*/
		
		
		return contentPane;
	}

Try using the GridBagLayout colWeights and rowWeights arrays, rather than assigning per item. I haven't lloked closely, but I already see a weighty of 1.0 on row 0 and 1 and a 0.1 on row 2, that means you are attemtping to use 210% of the space. Just because you are giving those values to a single component, they apply to the entire row, not just the component. That means it is easier, less error-prone, and more effective to simply set weightx and y to 0.0 and use the Layout's arrays. Try that first (as shown in the example).

After tons of fiddling I got the layout to work!!! Thank you so much for your help I now have a better understanding of weights and the GridBagLayout in general.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.