Results 1 to 5 of 5
  1. #1
    jchinson is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default Positioning items with GridBagConstraints

    Can anybody tell me why the following code sticks all of my items in the center of the window?

    Java Code:
    public static void mainScreen() {
    
    		JFrame frame = new JFrame();
    		pane = frame.getContentPane();
    		JButton addBtn = new JButton("Add Kid");
    		JLabel cabinLabel = new JLabel("Select a cabin");
    		String[] cabins = {"Cabin 1", "Cabin 2", "Cabin 3", "Cabin 4", 
    				"Cabin 5", "Cabin 6", "Cabin 7", "Cabin 8", "Cabin 9", "Cabin 10"};
    		JComboBox cabinList = new JComboBox(cabins);
    		
    		frame.setTitle("WOW");
    		frame.setSize(700,400);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		
    		pane.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.weightx = 0.5;
    		c.gridx = 0;
    		c.gridy = 0;
    		pane.add(cabinLabel, c);
    		
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.weightx = 0.5;
    		c.gridx = 0;
    		c.gridy = 1;
    		c.insets = new Insets(0,0,0,200);
    		pane.add(cabinList, c);
    		
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.weightx = 0.5;
    		c.gridx = 0;
    		c.gridy = 2;
    		pane.add(list, c);
    		
    		pane.add(addBtn);
    		frame.validate();
    		
    		addBtn.setMnemonic(KeyEvent.VK_A);
    		addBtn.setActionCommand("Add new kid");
    I want to push them all up closer to the top. How do I accomplish this?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You carefully use GridBagConstraints when adding components but don't when adding the addBtn, why?
    What do you wish your GUI to look like and what does it currently look like instead? Images would be helpful.
    Create and post a small compilable program that demonstrates your problem. In your case here, all you'd need to do would be to declare some variables that you're using display the JFrame and add a main method to call this method.

  3. #3
    jchinson is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    Just hadn't gotten around to doing it for the button yet. I've attached an image of what it currently looks like. I want the label, dropdown box, and button all moved up instead of centered. Here is the code

    Java Code:
    public class WOW {
    	public static final int MAX_KIDS = 500;
    	static Hashtable<String, Kid> kids = new Hashtable<String, Kid>(MAX_KIDS);
    	static JList list = new JList();
    	static Container pane = new Container();
    	
    	public static void main(String[] args) {
    		Kid kid1 = new Kid("Cody", 20, 3, "", "", "", "", "", "", "", "", "","","","");
    		Kid kid2 = new Kid("Joey", 20, 3, "", "", "", "", "", "", "", "", "","","","");
    		Kid kid3 = new Kid("George", 20, 3, "", "", "", "", "", "", "", "", "","","","");
    		Kid kid4 = new Kid("Daniel", 20, 3, "", "", "", "", "", "", "", "", "","","","");
    		kids.put(kid1.getName(), kid1);
    		kids.put(kid2.getName(), kid2);
    		kids.put(kid3.getName(), kid3);
    		kids.put(kid4.getName(), kid4);
    		mainScreen();
    	
    	public static void mainScreen() {
    
    		JFrame frame = new JFrame();
    		pane = frame.getContentPane();
    		JButton addBtn = new JButton("Add Kid");
    		JLabel cabinLabel = new JLabel("Select a cabin");
    		String[] cabins = {"Cabin 1", "Cabin 2", "Cabin 3", "Cabin 4", 
    				"Cabin 5", "Cabin 6", "Cabin 7", "Cabin 8", "Cabin 9", "Cabin 10"};
    		JComboBox cabinList = new JComboBox(cabins);
    		
    		frame.setTitle("WOW");
    		frame.setSize(700,400);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		
    		pane.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.weightx = 0.5;
    		c.gridx = 0;
    		c.gridy = 0;
    		pane.add(cabinLabel, c);
    		
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.weightx = 0.5;
    		c.gridx = 0;
    		c.gridy = 1;
    		c.insets = new Insets(0,0,0,200);
    		pane.add(cabinList, c);
    		
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.weightx = 0.5;
    		c.gridx = 0;
    		c.gridy = 2;
    		pane.add(list, c);
    		
    		pane.add(addBtn);
    		frame.validate();
    		
    		addBtn.setMnemonic(KeyEvent.VK_A);
    		addBtn.setActionCommand("Add new kid");
    		
    		//Action Listener for Add Button
    		addBtn.addActionListener(
    				new ActionListener() {
    					public void actionPerformed(ActionEvent e) {
    						addKid();
    					}
    				});
    		
    		//Action Listener for Cabin List
    		cabinList.addActionListener(
    				new ActionListener() {
    					public void actionPerformed(ActionEvent e) {
    						JComboBox cabinList = (JComboBox)e.getSource();
    						String cabinSelected = (String)cabinList.getSelectedItem();
    						
    						updateCabinList(cabinSelected);
    						list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    						list.setLayoutOrientation(JList.VERTICAL);
    						
    						//frame.validate();
    					}
    				});
    	
    		
    	}
    Attached Thumbnails Attached Thumbnails Positioning items with GridBagConstraints-untitled.jpg  

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code doesn't compile for us and has code inside of it completely unrelated to the problem. If you're still having problems, please remove all references to Kid or any other non-essential classes (non-essential for your problem at hand), and just create a simple GUI that shows your layout issue. The idea is to create a very small class that we can run and modify and hopefully be able to help you with. Also what do you mean by "all moved up"? Again, a picture is worth a thousand words.
    Last edited by Fubarable; 07-04-2011 at 11:39 PM.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Can anybody tell me why the following code sticks all of my items in the center of the window?
    See: How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    You will find a comment that says:

    Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container.

    which would appear to be your problem.

Similar Threads

  1. Position 5 buttons with GridBagConstraints in Swing
    By albertkao in forum AWT / Swing
    Replies: 0
    Last Post: 02-21-2011, 04:15 PM
  2. GridBagConstraints help
    By noobgrammer in forum New To Java
    Replies: 5
    Last Post: 07-03-2010, 05:12 AM
  3. [SOLVED] [newbie] java.awt.GridBagConstraints
    By jon80 in forum New To Java
    Replies: 0
    Last Post: 05-27-2009, 04:40 PM
  4. Positioning using setBounds
    By thayalan in forum AWT / Swing
    Replies: 4
    Last Post: 04-28-2009, 02:59 AM
  5. GridBagConstraints
    By Aswq in forum New To Java
    Replies: 2
    Last Post: 07-21-2008, 10:08 PM

Posting Permissions

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