Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java GridBagLayout - Any ideas?

  • 29-10-2007 07:46PM
    #1
    Closed Accounts Posts: 324 ✭✭


    Hi Please help with this GridBagLayout I am doing (in java).
    All is ok, except the spacing. This code won't compile on its own but you can see the problem in the image - all of the components on the first line are spaced out.

    Ideally they would be one after the other from left to right with only 10 pixels between each of them.

    btw I have to do this manually for an assignment....:mad:

    JPanel searchPanel = new JPanel();
    	searchPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Search"), BorderFactory
    		.createEmptyBorder(5, 5, 5, 5)));
    			
    		
    	// Set up the gbl for the search panel
    	GridBagLayout gbl = new GridBagLayout();
    	searchPanel.setLayout(gbl);
    		
    	GridBagConstraints gbc = new GridBagConstraints();
    	
    	// Define all of the components
    	JLabel search = new JLabel("Search for: ");
    	// searchText is already done
    	JLabel searchIn = new JLabel("In: ");
    	searchFields = new JComboBox(new String[] {"Name", 
    				                       "First Name",						          "Last Name",
    					          "Team",							          "County"}); 
    	// searchButton is a JButton		
    	gbc.gridx = 0;
    	gbc.gridy = 0;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 1.0;
    	gbc.weighty = 1.0;
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.ipadx = 0; 
    	gbc.insets = new Insets(1, 5, 0, 10);
    	gbl.setConstraints(search, gbc);
    	searchPanel.add(search);
    		
    	gbc.gridx = 1;
    	gbc.gridy = 0;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 0.0;
    	gbc.weighty = 0; 
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(1, 1, 0, 10);
    	gbl.setConstraints(searchText, gbc);
    	searchPanel.add(searchText);
    	
    	gbc.gridx = 2;
    	gbc.gridy = 0;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 1.0;
    	gbc.weighty = 0; 
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(1, 1, 0, 10);
    	gbl.setConstraints(searchIn, gbc);
    	searchPanel.add(searchIn);
    	
    	gbc.gridx = 3;
    	gbc.gridy = 0;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 1.0;
    	gbc.weighty = 0; 
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(1, 1, 0, 10);
    	gbl.setConstraints(searchFields, gbc);
    	searchPanel.add(searchFields);
    	
    	gbc.gridx = 4;
    	gbc.gridy = 0;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 1.0;
    	gbc.weighty = 0; // already defined
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(1, 1, 0, 40);
    	gbl.setConstraints(searchButton, gbc);
    	searchPanel.add(searchButton);
    	
    	
    	gbc.gridx = 0;
    	gbc.gridy = 1;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 0;
    	gbc.weighty = 1.0; 
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(0, 0, 0, 0);
    	gbl.setConstraints(exactMatch, gbc);
    	searchPanel.add(exactMatch);
    		
    		
    	caseSensitive = new JCheckBox("Case Sensitive");
    	gbc.gridx = 0;
    	gbc.gridy = 2;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 0;
    	gbc.weighty = 1.0; 
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(0, 0, 0, 0);
    	gbl.setConstraints(caseSensitive, gbc);
    	searchPanel.add(caseSensitive);
    	
    	JButton advanced = new JButton("Advanced Search...");
    	gbc.gridx = 0;
    	gbc.gridy = 3;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 0; 
    	gbc.weighty = 1.0; 
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(0, 0, 0, 0);
    	gbl.setConstraints(advanced, gbc);
    	searchPanel.add(advanced);
    	
                 searchPanel.setPreferredSize(new Dimension(850, 160));
    


Comments

  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Hi Please help with this GridBagLayout I am doing (in java).
    All is ok, except the spacing. This code won't compile on its own but you can see the problem in the image - all of the components on the first line are spaced out.

    Ideally they would be one after the other from left to right with only 10 pixels between each of them.

    btw I have to do this manually for an assignment....:mad:

    JPanel searchPanel = new JPanel();
    	searchPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Search"), BorderFactory
    		.createEmptyBorder(5, 5, 5, 5)));
    			
    		
    	// Set up the gbl for the search panel
    	GridBagLayout gbl = new GridBagLayout();
    	searchPanel.setLayout(gbl);
    		
    	GridBagConstraints gbc = new GridBagConstraints();
    	
    	// Define all of the components
    	JLabel search = new JLabel("Search for: ");
    	// searchText is already done
    	JLabel searchIn = new JLabel("In: ");
    	searchFields = new JComboBox(new String[] {"Name", 
    				                       "First Name",						          "Last Name",
    					          "Team",							          "County"}); 
    	// searchButton is a JButton		
    	gbc.gridx = 0;
    	gbc.gridy = 0;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 1.0;
    	gbc.weighty = 1.0;
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.ipadx = 0; 
    	gbc.insets = new Insets(1, 5, 0, 10);
    	gbl.setConstraints(search, gbc);
    	searchPanel.add(search);
    		
    	gbc.gridx = 1;
    	gbc.gridy = 0;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 0.0;
    	gbc.weighty = 0; 
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(1, 1, 0, 10);
    	gbl.setConstraints(searchText, gbc);
    	searchPanel.add(searchText);
    	
    	gbc.gridx = 2;
    	gbc.gridy = 0;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 1.0;
    	gbc.weighty = 0; 
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(1, 1, 0, 10);
    	gbl.setConstraints(searchIn, gbc);
    	searchPanel.add(searchIn);
    	
    	gbc.gridx = 3;
    	gbc.gridy = 0;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 1.0;
    	gbc.weighty = 0; 
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(1, 1, 0, 10);
    	gbl.setConstraints(searchFields, gbc);
    	searchPanel.add(searchFields);
    	
    	gbc.gridx = 4;
    	gbc.gridy = 0;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 1.0;
    	gbc.weighty = 0; // already defined
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(1, 1, 0, 40);
    	gbl.setConstraints(searchButton, gbc);
    	searchPanel.add(searchButton);
    	
    	
    	gbc.gridx = 0;
    	gbc.gridy = 1;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 0;
    	gbc.weighty = 1.0; 
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(0, 0, 0, 0);
    	gbl.setConstraints(exactMatch, gbc);
    	searchPanel.add(exactMatch);
    		
    		
    	caseSensitive = new JCheckBox("Case Sensitive");
    	gbc.gridx = 0;
    	gbc.gridy = 2;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 0;
    	gbc.weighty = 1.0; 
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(0, 0, 0, 0);
    	gbl.setConstraints(caseSensitive, gbc);
    	searchPanel.add(caseSensitive);
    	
    	JButton advanced = new JButton("Advanced Search...");
    	gbc.gridx = 0;
    	gbc.gridy = 3;
    	gbc.gridwidth = 1;
    	gbc.gridheight = 1;
    	gbc.weightx = 0; 
    	gbc.weighty = 1.0; 
    	gbc.fill = GridBagConstraints.NONE;
    	gbc.anchor = GridBagConstraints.NORTHWEST;
    	gbc.insets = new Insets(0, 0, 0, 0);
    	gbl.setConstraints(advanced, gbc);
    	searchPanel.add(advanced);
    	
                 searchPanel.setPreferredSize(new Dimension(850, 160));
    

    Yeah, that was one of the things about Java that really annoyed me. Using netbeans or something is a breeze, but if your assignment says you gotta do it manually, you gotta just keep hacking till you figure it out. Is there some absolute positioning in java? If I remember correctly, absolute positioning was even more ugly than GridBagLayout...


  • Closed Accounts Posts: 324 ✭✭radioactiveman


    hi - I found a way around it.
    Had to put the top row of components into a JPanel of their own.. the GridBagLayout is just a grid no matter what you do + that's what was spreading things out....
    I don't think there is a layout manager that is capable of doing this simply. won't be going near it again anyway if I can help it:D


Advertisement