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
Java GridBagLayout - Any ideas?
Options
-
29-10-2007 7:46pmHi 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));
0
Comments
-
radioactiveman wrote: »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...0 -
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:D0
Advertisement