component positioning spacing problem
can anyone help me how I can position those buttons so that they can have spaces among them?
I have the following code with GridBagLayout. where I have Five Buttons. I tried to positioned them so that they have spaces among each other. But I couldnt add any spaces among them. Please help
Code:
package test.gui;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class layer1 extends JPanel {
public layer1() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
JButton btnNewButton = new JButton("button1");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnActionPerformed (e);
}
});
btnNewButton.setIcon(new ImageIcon("Images/pic1.gif"));
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton.gridx = 13;
gbc_btnNewButton.gridy = 3;
add(btnNewButton, gbc_btnNewButton);
JButton btnNewButton_1 = new JButton("button2");
btnNewButton_1.setIcon(new ImageIcon("Images/pic2.gif"));
GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints();
gbc_btnNewButton_1.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton_1.gridx = 17;
gbc_btnNewButton_1.gridy = 3;
add(btnNewButton_1, gbc_btnNewButton_1);
JButton btnNewButton_4 = new JButton("button middle");
btnNewButton_4.setIcon(new ImageIcon("Images/pic3.gif"));
GridBagConstraints gbc_btnNewButton_4 = new GridBagConstraints();
gbc_btnNewButton_4.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton_4.gridx = 15;
gbc_btnNewButton_4.gridy = 9;
add(btnNewButton_4, gbc_btnNewButton_4);
JButton btnNewButton_2 = new JButton("button3");
btnNewButton_2.setIcon(new ImageIcon("Images/pic4.gif"));
GridBagConstraints gbc_btnNewButton_2 = new GridBagConstraints();
gbc_btnNewButton_2.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton_2.gridx = 13;
gbc_btnNewButton_2.gridy = 12;
add(btnNewButton_2, gbc_btnNewButton_2);
JButton btnNewButton_3 = new JButton("button4");
btnNewButton_3.setIcon(new ImageIcon("Images/pic5.gif"));
GridBagConstraints gbc_btnNewButton_3 = new GridBagConstraints();
gbc_btnNewButton_3.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton_3.gridx = 17;
gbc_btnNewButton_3.gridy = 12;
add(btnNewButton_3, gbc_btnNewButton_3);
}
private void btnActionPerformed (ActionEvent evt) {
removeAll ();
// layerTwo createLayerTwo = new layerTwo ();
test createLayerTwo = new test ();
add (createLayerTwo, new GridBagLayout ());
validate ();
}
}
Code:
package test.gui;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JTabbedPane;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import java.awt.GridBagConstraints;
//public class tabs extends JPanel {
public class tabs extends JFrame {
JFrame myFrame = null;
public static void main(String [] args) {
new tabs();
}
public tabs() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0};
gridBagLayout.rowHeights = new int[]{0, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
GridBagConstraints gbc_tabbedPane = new GridBagConstraints();
gbc_tabbedPane.fill = GridBagConstraints.BOTH;
gbc_tabbedPane.gridx = 0;
gbc_tabbedPane.gridy = 0;
add(tabbedPane, gbc_tabbedPane);
JPanel panelRoom1 = new JPanel();
panelRoom1.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 2), new CompoundBorder()));
tabbedPane.addTab("tab1", null, panelRoom1, null);
layer1 createLayer1 = new layer1 ();
panelRoom1.add(createLayer1);
JPanel panel_1 = new JPanel();
tabbedPane.addTab("tab2", null, panel_1, null);
test createLayerTwo = new test ();
panel_1.add(createLayerTwo);
JPanel panel_2 = new JPanel();
tabbedPane.addTab("tab3", null, panel_2, null);
setPreferredSize(new Dimension(1378,768));
pack();
setVisible(true);
}
}
Re: component positioning spacing problem
Moved from New to Java
db
Re: component positioning spacing problem
Re: component positioning spacing problem
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. Your problem is achieving a GridBagLayout with spaces between buttons, so post code that makes a GUI with only 2 to 4 buttons in a GBL.
db
Re: component positioning spacing problem
Quote:
Originally Posted by
DarrylBurke
ok I modified my code and made the code with two buttons. Please help me figure out how I can add horizontal and vertical spaces. Here is my new code.
Code:
package test.gui;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class layer1 extends JPanel {
public layer1() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
JButton btnNewButton_2 = new JButton("button3");
btnNewButton_2.setIcon(new ImageIcon("Images/pic4.gif"));
GridBagConstraints gbc_btnNewButton_2 = new GridBagConstraints();
gbc_btnNewButton_2.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton_2.gridx = 13;
gbc_btnNewButton_2.gridy = 12;
add(btnNewButton_2, gbc_btnNewButton_2);
JButton btnNewButton_3 = new JButton("button4");
btnNewButton_3.setIcon(new ImageIcon("Images/pic5.gif"));
GridBagConstraints gbc_btnNewButton_3 = new GridBagConstraints();
gbc_btnNewButton_3.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton_3.gridx = 17;
gbc_btnNewButton_3.gridy = 12;
add(btnNewButton_3, gbc_btnNewButton_3);
}
}
Code:
package test.gui;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JTabbedPane;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import java.awt.GridBagConstraints;
public class tabs extends JFrame {
JFrame myFrame = null;
public static void main(String [] args) {
new tabs();
}
public tabs() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0};
gridBagLayout.rowHeights = new int[]{0, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
GridBagConstraints gbc_tabbedPane = new GridBagConstraints();
gbc_tabbedPane.fill = GridBagConstraints.BOTH;
gbc_tabbedPane.gridx = 0;
gbc_tabbedPane.gridy = 0;
add(tabbedPane, gbc_tabbedPane);
JPanel panelRoom1 = new JPanel();
panelRoom1.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 2), new CompoundBorder()));
tabbedPane.addTab("tab1", null, panelRoom1, null);
layer1 createLayer1 = new layer1 ();
panelRoom1.add(createLayer1);
JPanel panel_1 = new JPanel();
tabbedPane.addTab("tab2", null, panel_1, null);
layer1 createLayer1 = new layer1 ();
panel_1.add(createLayer1);
JPanel panel_2 = new JPanel();
tabbedPane.addTab("tab3", null, panel_2, null);
setPreferredSize(new Dimension(1378,768));
pack();
setVisible(true);
}
}
Re: component positioning spacing problem
GridBagLayout does not reserve space for rows and columns that contain no components.
db