Results 1 to 6 of 6
  1. #1
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default 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
    Java 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 ();
    		
    	}
    
    }
    Java 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);
    
    	}
    
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    10,093
    Rep Power
    17

    Default Re: component positioning spacing problem

    Moved from New to Java

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

  3. #3
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Re: component positioning spacing problem

    someone please help

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    10,093
    Rep Power
    17

    Default 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
    Why do they call it rush hour when nothing moves? - Robin Williams

  5. #5
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Re: component positioning spacing problem

    Quote Originally Posted by DarrylBurke View Post
    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
    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.
    Java 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);
    
    	}
    }
    Java 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);
    
    	}
    
    }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    10,093
    Rep Power
    17

    Default Re: component positioning spacing problem

    GridBagLayout does not reserve space for rows and columns that contain no components.

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Similar Threads

  1. A componenet positioning problem
    By apprerntice in forum AWT / Swing
    Replies: 10
    Last Post: 07-22-2011, 07:53 PM
  2. [Q] Spacing Help
    By iriscience in forum New To Java
    Replies: 7
    Last Post: 02-08-2011, 01:44 AM
  3. problem positioning a JPanel in a JScrollPane
    By gib65 in forum AWT / Swing
    Replies: 4
    Last Post: 08-02-2010, 10:34 AM
  4. Spacing/Alignment problem with java 2D printing
    By myselfmayur in forum Java 2D
    Replies: 2
    Last Post: 06-25-2010, 03:37 PM
  5. Problem with margins/spacing
    By joseche in forum AWT / Swing
    Replies: 2
    Last Post: 12-26-2008, 08:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •