Reputation: 1297
How can I remove spacing caused by gridBagLayout and make them stick together?
Here is my code simply adding 3 buttons.
I read this question but there was no complete solution How to fix gap in GridBagLayout;
I just want to put all of my buttons on the top of the JFrame.
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyProblem {
private JFrame frame = new JFrame();
public static void main(String[] args) {
new MyProblem();
}
public MyProblem() {
frame.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.weightx = 1;
gc.weighty = 1;
gc.gridx = 0;
gc.gridy = 0;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.anchor = GridBagConstraints.NORTH;
for (int i = 0; i < 3; i++) {
JPanel jPanel = new JPanel(new BorderLayout());
jPanel.setSize(80, 80);
jPanel.add(new JButton("Button " + i),BorderLayout.PAGE_START);
frame.add(jPanel, gc);
gc.gridy++;
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
How my buttons look like:
How I want my buttons to look like:
Upvotes: 3
Views: 1987
Reputation: 1297
This code will do the job:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MyProblem {
private JFrame frame = new JFrame();
public static void main(String[] args) {
new MyProblem();
}
public MyProblem() {
frame.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.weightx = 1;
gc.weighty = 0;
gc.gridx = 0;
gc.gridy = 0;
gc.ipadx = 0;
gc.ipady = 0;
gc.fill = GridBagConstraints.HORIZONTAL;
gc.anchor = GridBagConstraints.NORTH;
for (int i = 0; i < 3; i++) {
JButton button = new JButton("Button " + i);
frame.add(button, gc);
gc.gridy++;
}
gc.weighty = 1;
frame.add(Box.createGlue(), gc); //Adding a component to feel the area.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
Upvotes: 3
Reputation: 168825
The layout makes a single column of buttons, so..
Change:
gc.fill = GridBagConstraints.HORIZONTAL;
To:
gc.fill = GridBagConstraints.BOTH;
I want to keep buttons to the same height as described in picture and just putting them at the top.
To constrain them to the top is fairly easy using a combined layout. In this case we might add the panel with the buttons into the PAGE_START
of a panel with BorderLayout
. That part of a border layout will stretch the width of the child component (our panel with GridLayout
) to fill the available space, but it will respect the height of whatever is in it - giving the component only as much vertical space as it needs.
Here is an MCVE that implements the idea described above. The outer panel (with a cyan colored border) is used to constrain the height of the button panel (with orange border). See the comments in the source for more detail on how it works.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class MyProblem {
private JFrame frame = new JFrame();
public static void main(String[] args) {
new MyProblem();
}
public MyProblem() {
frame.setLayout(new BorderLayout()); // actually the default
// we will use this panel to constrain the panel with buttons
JPanel ui = new JPanel(new BorderLayout());
frame.add(ui);
ui.setBorder(new LineBorder(Color.CYAN, 3));
// the panel (with GridLayout) for the buttons
JPanel toolPanel = new JPanel(new GridLayout(0,1,0,4)); // added some gap
toolPanel.setBorder(new LineBorder(Color.ORANGE, 3));
// toolPanel will appear at the top of the ui panel
ui.add(toolPanel, BorderLayout.PAGE_START);
for (int i = 0; i < 3; i++) {
toolPanel.add(new JButton("Button " + i));
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(500, 500); // instead..
frame.pack(); // pack will make it as small as it can be.
frame.setMinimumSize(frame.getSize()); // nice tweak..
frame.setVisible(true);
}
}
Upvotes: 4
Reputation: 453
Run this code I think you will get help from it
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Think {
public static void addComponentsToPane(Container pane) {
JButton jbnButton;
pane.setLayout(new GridBagLayout());
GridBagConstraints gBC = new GridBagConstraints();
gBC.fill = GridBagConstraints.HORIZONTAL;
jbnButton = new JButton("Button 1");
gBC.weightx = 0.5;
gBC.gridx = 0;
gBC.gridy = 0;
pane.add(jbnButton, gBC);
jbnButton = new JButton("Button 3");
gBC.gridx = 2;
gBC.gridy = 0;
pane.add(jbnButton, gBC);
jbnButton = new JButton("Button 4");
gBC.ipady = 40; //This component has more breadth compared to other buttons
gBC.weightx = 0.0;
gBC.gridwidth = 3;
gBC.gridx = 0;
gBC.gridy = 1;
pane.add(jbnButton, gBC);
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("GridBagLayout Source Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Upvotes: 0