Results 1 to 4 of 4
  1. #1
    Serpenthelm is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default GridBagLayout - forcing all components to stay on top

    I'm experimenting with the standard Java Layouts, and I'm really sorry to say that I'd like to try custom ones, but can't.

    So I'm going through the not-so-pleasing experience of trying to get GridBagLayout to work as I want.

    I modded the Sun example to fit my needs, but I can't get the buttons to stay together on top of the Layout.
    I wonder if that's even possible without setting too many hand-picked constraints and/or making the buttons resize
    vertically (they would become too tall).

    This is what happens when I resize the window. I'd like to see the red space go away, and the buttons to keep their vertical size.



    Here's the code. I set all the weights to 1.0 because I didn't want to bother with that mess.
    Java Code:
    import java.awt.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class GridBagLayoutDemo {
    	final static boolean shouldFill = true;
    	final static boolean shouldWeightX = true;
    	final static boolean RIGHT_TO_LEFT = false;
    
    	public static void addComponentsToPane(Container pane) {
    		if (RIGHT_TO_LEFT) {
    			pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    		}
    
    		JButton button; // temp
    		pane.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    
    		if (shouldFill) {
    			// natural height, maximum width
    			c.fill = GridBagConstraints.HORIZONTAL;
    		}
    
    		button = new JButton("Button 1");
    		if (shouldWeightX) {
    			c.weightx = 1.0;
    			c.weighty = 1.0; // request any extra vertical space
    			c.anchor = GridBagConstraints.PAGE_START; // top of space
    		}
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 0;
    		c.gridy = 0;
    		pane.add(button, c);
    
    		button = new JButton("Button 2");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.weightx = 1.0;
    		c.weighty = 1.0; // request any extra vertical space
    		c.anchor = GridBagConstraints.PAGE_START; // top of space
    		c.gridx = 1;
    		c.gridy = 0;
    		pane.add(button, c);
    
    		button = new JButton("Button 3");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.weightx = 1.0;
    		c.weighty = 1.0; // request any extra vertical space
    		c.anchor = GridBagConstraints.PAGE_START; // top of space
    		c.gridx = 2;
    		c.gridy = 0;
    		pane.add(button, c);
    
    		button = new JButton("Long-Named Button 4");
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.ipady = 40; // make this component tall
    		c.weightx = 1.0;
    		c.weighty = 1.0; // request any extra vertical space
    		c.anchor = GridBagConstraints.PAGE_START; // top of space
    		c.gridwidth = 3;
    		c.gridx = 0;
    		c.gridy = 1;
    		pane.add(button, c);
    	}
    
    	/**
    	 * Create the GUI and show it. For thread safety, this method should be
    	 * invoked from the event-dispatching thread.
    	 */
    	private static void createAndShowGUI() {
    		// Create and set up the window.
    		JFrame frame = new JFrame("GridBagLayoutDemo");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		// Set up the content pane.
    		addComponentsToPane(frame.getContentPane());
    
    		// Display the window.
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    	public static void main(String[] args) {
    		// Schedule a job for the event-dispatching thread:
    		// creating and showing this application's GUI.
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				createAndShowGUI();
    			}
    		});
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: GridBagLayout - forcing all components to stay on top

    I think I understand what you want, and if so, the solution is to nest layouts. Place your components into a JPanel that uses GridBagLayout, and then place that component into a container that uses BorderLayout in the BorderLayout.PAGE_START or BorderLayout.NORTH position (same thing). It just so happens that the JFrame's contentPane uses BorderLayout by default, so you can do it like so:

    Java Code:
    import java.awt.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class GridBagLayoutDemo2 {
       final static boolean shouldFill = true;
       final static boolean shouldWeightX = true;
       final static boolean RIGHT_TO_LEFT = false;
    
       public static void addComponentsToPane(Container pane) {
    
          // create a new JPanel that will use GridBagLayout
          JPanel gridBagPanel = new JPanel();
          if (RIGHT_TO_LEFT) {
             gridBagPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
          }
    
          JButton button;
          gridBagPanel.setLayout(new GridBagLayout());
          GridBagConstraints c = new GridBagConstraints();
    
          if (shouldFill) {
    
             c.fill = GridBagConstraints.HORIZONTAL;
          }
    
          button = new JButton("Button 1");
          if (shouldWeightX) {
             c.weightx = 1.0;
             c.weighty = 1.0;
             c.anchor = GridBagConstraints.PAGE_START;
          }
          c.fill = GridBagConstraints.HORIZONTAL;
          c.gridx = 0;
          c.gridy = 0;
          gridBagPanel.add(button, c);
    
          button = new JButton("Button 2");
          c.fill = GridBagConstraints.HORIZONTAL;
          c.weightx = 1.0;
          c.weighty = 1.0;
          c.anchor = GridBagConstraints.PAGE_START;
          c.gridx = 1;
          c.gridy = 0;
          gridBagPanel.add(button, c);
    
          button = new JButton("Button 3");
          c.fill = GridBagConstraints.HORIZONTAL;
          c.weightx = 1.0;
          c.weighty = 1.0;
          c.anchor = GridBagConstraints.PAGE_START;
          c.gridx = 2;
          c.gridy = 0;
          gridBagPanel.add(button, c);
    
          button = new JButton("Long-Named Button 4");
          c.fill = GridBagConstraints.HORIZONTAL;
          c.ipady = 40;
          c.weightx = 1.0;
          c.weighty = 1.0;
          c.anchor = GridBagConstraints.PAGE_START;
          c.gridwidth = 3;
          c.gridx = 0;
          c.gridy = 1;
          gridBagPanel.add(button, c);
    
          // and then add the gridbag panel to the contentPane at the top
          pane.add(gridBagPanel, BorderLayout.PAGE_START);
       }
    
       private static void createAndShowGUI() {
    
          JFrame frame = new JFrame("GridBagLayoutDemo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
          addComponentsToPane(frame.getContentPane());
    
          frame.pack();
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
    
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGUI();
             }
          });
       }
    }

  3. #3
    Serpenthelm is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Re: GridBagLayout - forcing all components to stay on top

    Ok, it works, I guess I can remove this code and it will work anyway.
    Java Code:
    c.weighty = 1.0;
    c.anchor = GridBagConstraints.PAGE_START;
    Is there a way to do this using just GridBagLayout?

    EDIT: wait, we are creating a new panel just to put it inside the top of another panel?
    Last edited by Serpenthelm; 01-03-2012 at 05:25 PM.

  4. #4
    Serpenthelm is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Re: GridBagLayout - forcing all components to stay on top

    I'm trying to add a labeled JTextField to a cell of the GridBagLayout.
    So I put the JLabel and the JTextField inside a Box and then I put that
    Box inside a cell, but there is a misalignment on the left.

    How can I solve this? I tried setHorizontalAlignment(), but it does nothing.



    Java Code:
    package sandbox;
    
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.GridBagConstraints;
    
    import javax.swing.Box;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    
    public class GridBagLayoutDemo2 {
    	
    	public static void addComponentsToPane(Container pane) {
    		JPanel gridBagPanel = new JPanel();
    		Box myBox = Box.createVerticalBox();
    		
    		JLabel myLabel = new JLabel("Some text");
    //		myLabel.setHorizontalAlignment(SwingConstants.LEFT); // useless
    		myBox.add(myLabel);
    		
    		JTextField configUrlField = new JTextField("Some long text", 15);
    //		configUrlField.setHorizontalAlignment(SwingConstants.LEFT); // useless
    		myBox.add(configUrlField);
    		
    		GridBagConstraints myGBC = new GridBagConstraints();
    		myGBC.weightx = 1.0;
    		myGBC.weighty = 1.0;
    		myGBC.anchor = GridBagConstraints.PAGE_START;
    		myGBC.fill = GridBagConstraints.HORIZONTAL;
    		myGBC.gridx = 0;
    		myGBC.gridy = 0;
    
    		gridBagPanel.add(myBox, myGBC);
    		pane.add(gridBagPanel, BorderLayout.PAGE_START);
    	}
    	
    	private static void createAndShowGUI() {
    		 
    	      JFrame frame = new JFrame("GridBagLayoutDemo");
    	      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	 
    	      addComponentsToPane(frame.getContentPane());
    	 
    	      frame.pack();
    	      frame.setVisible(true);
    	   }
    	 
    	   public static void main(String[] args) {
    	 
    	      javax.swing.SwingUtilities.invokeLater(new Runnable() {
    	         public void run() {
    	            createAndShowGUI();
    	         }
    	      });
    	   }
    }

Similar Threads

  1. Replies: 16
    Last Post: 11-17-2010, 02:02 PM
  2. Adding and removing components from a GridBagLayout
    By peterhabe in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 11:13 PM
  3. Forcing the mouse to stay quiet
    By ProspectiveDeveloper in forum AWT / Swing
    Replies: 4
    Last Post: 05-03-2010, 12:17 AM
  4. Arrange components in a GridBagLayout
    By ze snow in forum New To Java
    Replies: 1
    Last Post: 02-27-2010, 03:22 PM
  5. gridbaglayout: increase/decrease size of components.
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 08:22 AM

Posting Permissions

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