Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java GridBagLayout - Any ideas?

  • 29-10-2007 6: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