0

I am using GridBagLayout to align components. Actually, i have two buttons which i want to align like this:

Desired layout:

https://i.sstatic.net/4yHu4.jpg

But the following code results in the following layout:

Resulted layout:

https://i.sstatic.net/PtCUt.png

My code:

 iconAdd = new ImageIcon(getClass().getResource("../images/add.png")); add = new JButton(iconAdd); add.setPreferredSize(new Dimension(130, 100)); add.setBorder(new LineBorder(Color.decode("#9b9999"), 1, true)); add.setCursor(Cursor.getPredefinedCursor(12)); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1; gbc.anchor = GridBagConstraints.WEST; gbc.insets = new Insets(5, 5, 5, 5); pane.add(add, gbc); iconSearch = new ImageIcon(getClass().getResource("../images/search.png")); search = new JButton(iconSearch); search.setCursor(Cursor.getPredefinedCursor(12)); search.setPreferredSize(new Dimension(130, 100)); search.setBorder(new LineBorder(Color.decode("#9b9999"), 1, true)); gbc.gridx++; gbc.anchor = GridBagConstraints.WEST; gbc.insets = new Insets(5, 5, 5, 5); pane.add(search, gbc); 

Any help would be highly appreciated.

1

2 Answers 2

1
import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; public class GridBagLayoutDemo extends JFrame{ GridBagLayoutDemo(){ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWeights = new double[]{1.0, 1.0, 1.0}; gridBagLayout.columnWidths = new int[]{0,0,300}; getContentPane().setLayout(gridBagLayout); JButton button1 = new JButton("Long Button"); GridBagConstraints c1 = new GridBagConstraints(); c1.fill = GridBagConstraints.HORIZONTAL; c1.weightx = 0.0; c1.gridwidth = 3; c1.gridx = 0; c1.gridy = 0; getContentPane().add(button1, c1); JButton button2 = new JButton("Button 2"); GridBagConstraints c2 = new GridBagConstraints(); c2.weightx = 0.5; c2.gridx = 0; c2.gridy = 1; getContentPane().add(button2, c2); JButton button3 = new JButton("Button 3"); GridBagConstraints c3 = new GridBagConstraints(); c3.weightx = 0.5; c3.gridx = 1; c3.gridy = 1; getContentPane().add(button3, c3); pack(); setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new GridBagLayoutDemo(); } }); } } 

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

It can not solve your problem because you did not post minimal reproducible example so on can only guess how this posted code is used. This demonstrates a working layout, similar to what you need. You need to figure out what's wrong with your code by comparing it to the working demo, or adopt the working demo to your needs.
Why you used weightx = 0.5 ?
You can remove this line. It will not change the desired layout. Note the columns constrains, and the fact that each button is added using a new instance of GridBagConstraints
1
gbc.weightx = 1; 

You are telling the layout to give extra space to each component. So essentially each component becomes half the size of the frame.

You really only want that constraint set for the second button, so it takes up all the remaining space.

Read the section from the Swing tutorial on How to Use GridBagLayout which explains how the weightx/y constraints should be used.

Also, an easier solution would be do just use a FlowLayout. Create a panel with a FlowLayout. Add the buttons to the panel. Then add the panel to the BorderLayout.PAGE_START of the frame.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.