Is there something about adding a JList to GridBagLayout that I am missing. I want to place a JList between a JLabel and a JTextArea. No matter what I do the list seems to stay in column 1.

I am only using GridBagLayout in the west pane(wP) I have tried messing with the weights and I am stumped. I must be missing something!!!

I just want everything in column 0


First code block is the layout. Second is the entire project so far.

Project6()
	{
		super("James W Project6");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		nP = new JPanel();
		nP.setLayout(new FlowLayout());
		nP.setBackground(new Color(85,107,47));
		npLbl = new JLabel("South Park Photo Album");
		npLbl.setFont(fifty);
		nP.add(npLbl);
		add(nP, BorderLayout.NORTH);

		sP = new JPanel();
		sP.setLayout(new GridLayout(1,2));
		sP.setBackground(new Color(85,107,47));
		spLbl = new JLabel("Select Font Size");
		spLbl.setFont(thirty);
		spLbl.setHorizontalTextPosition(SwingConstants.LEFT);
		sP.add(spLbl);
		jsl.setMajorTickSpacing(2);
		jsl.setPaintTicks(true);
		jsl.setPaintLabels(true);
		jsl.addChangeListener(this);
		sP.add(jsl);
		add(sP, BorderLayout.SOUTH);

// Problem is in here

		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		wP = new JPanel();
		wP.setLayout(gridbag);
		wP.setBackground(new Color(189,183,107));

		wpLbl = new JLabel("Sayings");
		wpLbl.setFont(twenty);
		constraints.ipady = 50;
		constraints.weighty = 0.3;
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.anchor = GridBagConstraints.FIRST_LINE_START;
		gridbag.setConstraints(wpLbl, constraints);
		wP.add(wpLbl);

		sayList = new JList(sayings);
		sayList.setVisibleRowCount(3);	
		sayList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		constraints.weightx = 0.0;
		constraints.weighty = 0.0;
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.anchor = GridBagConstraints.LINE_START;
		gridbag.setConstraints(sayList, constraints);
		wP.add(new JScrollPane(sayList));
		sayList.addListSelectionListener(this);


		cart =  new JTextArea(11,12);
		cart.setBackground(new Color(189,183,107));
		cart.setBorder(BorderFactory.createTitledBorder
						(BorderFactory.createLineBorder(Color.black),"Cart:"));
		cart.setEditable(false);
		cart.setLineWrap(true);
		constraints.gridx = 0;
		constraints.gridy = 2;
		//constraints.anchor = GridBagConstraints.PAGE_END;
		gridbag.setConstraints(cart, constraints);
		wP.add(cart);
		add(wP, BorderLayout.WEST);






		eP = new JPanel();
		eP.setLayout(new GridLayout(12,1));
		eP.setBackground(new Color(189,183,107));
		lbl1 = new JLabel("Available");
		lbl1.setFont(twenty);
		lbl2 = new JLabel("Photo Sizes");
		lbl2.setFont(twenty);
		lbl3 = new JLabel("Check Selections");
		lbl3.setFont(twenty);
		cb3 = new JCheckBox("3X5     $0.50 each");
		cb5 = new JCheckBox("5X7     $1.00 each");
		cb6 = new JCheckBox("6X8     $1.50 each");
		cb8 = new JCheckBox("8X10    $3.00 each");
		cb16 = new JCheckBox("16X20   $5.00 each");
		completeBtn = new JButton("Complete Order");
		clrBtn = new JButton("Clear");
		exitBtn = new JButton("Exit");
		eP.add(lbl1);
		eP.add(lbl2);
		eP.add(lbl3);
		eP.add(cb3);
		eP.add(cb5);
		eP.add(cb6);
		eP.add(cb8);
		eP.add(cb16);
		eP.add(completeBtn);
		eP.add(clrBtn);
		eP.add(exitBtn);
		cb3.addItemListener(this);
		cb5.addItemListener(this);
		cb6.addItemListener(this);
		cb8.addItemListener(this);
		cb16.addItemListener(this);
		completeBtn.addActionListener(this);
		clrBtn.addActionListener(this);
		exitBtn.addActionListener(this);
		add(eP, BorderLayout.EAST);


/*   commented out for posting   uses icons   

		cP = new JPanel();
		cP.setLayout(new GridLayout(2,2));
		cP.setBackground(new Color(240,230,140));
		cpLbl1 = new JLabel("Photo Selection");
		cpLbl1.setFont(twenty);
		cP.add(cpLbl1);
		//picList = new JList(icons);
		//picList.setVisibleRowCount(2);
		//picList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		//cP.add(new JScrollPane(picList));
		//picList.addListSelectionListener(this);
		colorBtn = new JButton("Click to select a matte color and preview");
		cP.add(colorBtn);
		colorBtn.addActionListener(this);
		cP.add(cpLbl2);
		add(cP, BorderLayout.CENTER);
*/

	}
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;

public class Project6 extends JFrame implements MouseListener, ChangeListener,
												ListSelectionListener,
												ItemListener, ActionListener
{
	private Container c = getContentPane();
	private JPanel nP, sP, cP, eP, wP;
	private JLabel npLbl,spLbl,wpLbl,epLbl,cpLbl1, lbl1,lbl2,lbl3,lbl4 ,lbl5;
	private JLabel cpLbl2 = new JLabel("hey");
	private JLabel wpLbl1 = new JLabel("hey");
	private JSlider jsl = new JSlider(SwingConstants.HORIZONTAL,8,30,10);
	private JButton colorBtn,clrBtn, completeBtn, exitBtn;
	private JList sayList, picList;
	private JTextArea cart;
	private JCheckBox cb3, cb5, cb6, cb8, cb16;
	private Font fifty = new Font("Lucida Sans", Font.BOLD, 50);
	private Font thirty = new Font("Serif", Font.BOLD, 30);
	private Font twenty = new Font("Serif", Font.BOLD, 20);
	private Font six = new Font("Serif", Font.PLAIN, 6);
	private boolean sayPicked = false;
	private boolean sizePicked = false;
	private boolean mattePicked = false;
	private boolean imagePicked = false;

	private int size = 10;
	private int[] indices;
	private float total;
	private int qtyCb3, qtyCb5, qtyCb6, qtyCb8, qtyCb16;


	private String[] sayings = {"Honor Student","Varsity Football","Varsity Soccer",
								"Varsity Chearleading","Most likely to succeed",
								"Class Officer","Debate Team","Robotics Club",
								"School paper", "Loser"};

/* commented out for posting  in forum      incase someone wants to copy and paste to run it

	private String[] pics = {"1a.gif","2a.gif","3a.gif","4a.gif","50a.gif",
							  "62a.gif","98a.gif","64a.gif","56a.gif","7a.gif",
							  "57a.gif","49a.gif"};

	private String[] picNames = {"Eric","Kyle","Stan","Kenny","Starvin'Marvin","Timmy","Jimmy",
								 "Tweek","Phillip","Mr. Garrison","Terrance","Mr. Mackey"};

	private Icon icons[] = {new ImageIcon(pics[0]),
							new ImageIcon(pics[1]),
							new ImageIcon(pics[2]),
							new ImageIcon(pics[3]),
							new ImageIcon(pics[4]),
							new ImageIcon(pics[5]),
							new ImageIcon(pics[6]),
							new ImageIcon(pics[7]),
							new ImageIcon(pics[8]),
							new ImageIcon(pics[9]),
							new ImageIcon(pics[10]),
							new ImageIcon(pics[11])};
*/





	public void stateChanged(ChangeEvent ce)// Slider
	{
		size = jsl.getValue();
		System.out.println("Size is now set to " + size);

	}
	public void valueChanged(ListSelectionEvent lse)//List
	{
		if(lse.getSource() == sayList)
		{
			indices = sayList.getSelectedIndices();
			for(int i = 0; i<indices.length;i++)
			{
				System.out.println(sayings[indices[i]] + "\n\n");
			}
		}
		if(lse.getSource() == picList)
		{
			imagePicked = true;
			//System.out.println(picNames[picList.getSelectedIndex()]+ "     "+imagePicked);
		}

	}
	public void itemStateChanged(ItemEvent ie)//Checkboxes
	{
		if(ie.getSource() == cb3)
		{
			qtyCb3 = Integer.parseInt(JOptionPane.showInputDialog(
										"How many 3X5 pictures?"));

		}

	}
	public void actionPerformed(ActionEvent ae)//Buttons
	{
		if(ae.getSource() == colorBtn)
		{

		}
		if(ae.getSource() == clrBtn)
		{
			clearAll();
		}
		if(ae.getSource() == exitBtn)
		{
			System.exit(0);
		}
		if(ae.getSource() == completeBtn)
		{

		}

	}
	public void setSize()
	{

	}
	public void clearAll()
	{
		sayPicked = false;
		sizePicked = false;
		mattePicked = false;
		imagePicked = false;
		cb3.setSelected(false);
		cb5.setSelected(false);
		cb6.setSelected(false);
		cb8.setSelected(false);
		cb16.setSelected(false);
		jsl.setValue(10);
		sayList.clearSelection();
	}


	public static void main(String[] args)
	{
		Project6 a = new Project6();
		a.pack();
	    a.setVisible(true);
	}

	Project6()
	{
		super("James W Project6");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		nP = new JPanel();
		nP.setLayout(new FlowLayout());
		nP.setBackground(new Color(85,107,47));
		npLbl = new JLabel("South Park Photo Album");
		npLbl.setFont(fifty);
		nP.add(npLbl);
		add(nP, BorderLayout.NORTH);

		sP = new JPanel();
		sP.setLayout(new GridLayout(1,2));
		sP.setBackground(new Color(85,107,47));
		spLbl = new JLabel("Select Font Size");
		spLbl.setFont(thirty);
		spLbl.setHorizontalTextPosition(SwingConstants.LEFT);
		sP.add(spLbl);
		jsl.setMajorTickSpacing(2);
		jsl.setPaintTicks(true);
		jsl.setPaintLabels(true);
		jsl.addChangeListener(this);
		sP.add(jsl);
		add(sP, BorderLayout.SOUTH);

// Problem is in here

		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		wP = new JPanel();
		wP.setLayout(gridbag);
		wP.setBackground(new Color(189,183,107));

		wpLbl = new JLabel("Sayings");
		wpLbl.setFont(twenty);
		constraints.ipady = 50;
		constraints.weighty = 0.3;
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.anchor = GridBagConstraints.FIRST_LINE_START;
		gridbag.setConstraints(wpLbl, constraints);
		wP.add(wpLbl);

		sayList = new JList(sayings);
		sayList.setVisibleRowCount(3);	
		sayList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		constraints.weightx = 0.0;
		constraints.weighty = 0.0;
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.anchor = GridBagConstraints.LINE_START;
		gridbag.setConstraints(sayList, constraints);
		wP.add(new JScrollPane(sayList));
		sayList.addListSelectionListener(this);


		cart =  new JTextArea(11,12);
		cart.setBackground(new Color(189,183,107));
		cart.setBorder(BorderFactory.createTitledBorder
						(BorderFactory.createLineBorder(Color.black),"Cart:"));
		cart.setEditable(false);
		cart.setLineWrap(true);
		constraints.gridx = 0;
		constraints.gridy = 2;
		//constraints.anchor = GridBagConstraints.PAGE_END;
		gridbag.setConstraints(cart, constraints);
		wP.add(cart);
		add(wP, BorderLayout.WEST);






		eP = new JPanel();
		eP.setLayout(new GridLayout(12,1));
		eP.setBackground(new Color(189,183,107));
		lbl1 = new JLabel("Available");
		lbl1.setFont(twenty);
		lbl2 = new JLabel("Photo Sizes");
		lbl2.setFont(twenty);
		lbl3 = new JLabel("Check Selections");
		lbl3.setFont(twenty);
		cb3 = new JCheckBox("3X5     $0.50 each");
		cb5 = new JCheckBox("5X7     $1.00 each");
		cb6 = new JCheckBox("6X8     $1.50 each");
		cb8 = new JCheckBox("8X10    $3.00 each");
		cb16 = new JCheckBox("16X20   $5.00 each");
		completeBtn = new JButton("Complete Order");
		clrBtn = new JButton("Clear");
		exitBtn = new JButton("Exit");
		eP.add(lbl1);
		eP.add(lbl2);
		eP.add(lbl3);
		eP.add(cb3);
		eP.add(cb5);
		eP.add(cb6);
		eP.add(cb8);
		eP.add(cb16);
		eP.add(completeBtn);
		eP.add(clrBtn);
		eP.add(exitBtn);
		cb3.addItemListener(this);
		cb5.addItemListener(this);
		cb6.addItemListener(this);
		cb8.addItemListener(this);
		cb16.addItemListener(this);
		completeBtn.addActionListener(this);
		clrBtn.addActionListener(this);
		exitBtn.addActionListener(this);
		add(eP, BorderLayout.EAST);


/*   commented out for posting   uses icons   

		cP = new JPanel();
		cP.setLayout(new GridLayout(2,2));
		cP.setBackground(new Color(240,230,140));
		cpLbl1 = new JLabel("Photo Selection");
		cpLbl1.setFont(twenty);
		cP.add(cpLbl1);
		//picList = new JList(icons);
		//picList.setVisibleRowCount(2);
		//picList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		//cP.add(new JScrollPane(picList));
		//picList.addListSelectionListener(this);
		colorBtn = new JButton("Click to select a matte color and preview");
		cP.add(colorBtn);
		colorBtn.addActionListener(this);
		cP.add(cpLbl2);
		add(cP, BorderLayout.CENTER);
*/

	}
	public void mouseReleased(MouseEvent event){}
	public void mouseClicked(MouseEvent event){}
	public void mousePressed(MouseEvent event){}
	public void mouseEntered(MouseEvent event){}
	public void mouseExited(MouseEvent event){}


}

Recommended Answers

All 2 Replies

Have you tried using gridwidth or ipadx?

sure did everything works fine till I add the JList I can comment out the JList and use a JLabel and it works fine

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.