GridBagLayout Question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: GridBagLayout Question

  1. #1
    Join Date
    Apr 2006
    Posts
    6

    GridBagLayout Question

    Hey,
    Im using the GridBagLayout, and have a problem.

    When I resize the form, label1 and label2 stay put, which is good. But textbox1 and textbox2 move to the left, and expand. (It might be easier to download and run this code.)

    The problem is, the textfields move to the right, and I want them to stay where they are when the begin, but i still want their width to expand with the window.
    (E.G. I want x,y to stay the same, but width and height to expand with the container.)

    Heres the code:
    Code:
     import javax.swing.*;
    import java.awt.*;
    
    public class Main {
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        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;
            pane.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            if (shouldFill) {
                //natural height, maximum width
                c.fill = GridBagConstraints.HORIZONTAL;
            }
            
            JLabel label1 = new JLabel("Text: ");
            if (shouldWeightX) {
                c.weightx = 0.5;
            }
            c.gridx = 0;
            c.gridy = 0;
            c.insets = new Insets(5,0,0,0);
            pane.add(label1, c);
            
            JTextField textbox1 = new JTextField();
            c.gridx = 1;
            c.gridy = 0;
            c.insets = new Insets(5,0,0,0);
            pane.add(textbox1, c);
            
            JLabel label2= new JLabel("Encrypted: ");
            if (shouldWeightX) {
                c.weightx = 0.5;
            }
            c.gridx = 0;
            c.gridy = 1;
            c.insets = new Insets(5,0,0,0);
            pane.add(label2, c);
            
            JTextField textbox2 = new JTextField();
            c.gridx = 1;
            c.gridy = 1;
            c.insets = new Insets(5,0,0,0);
            pane.add(textbox2, c);
            
            button = new JButton("Encrypt");
            c.ipady = 0;       //reset to default
            c.anchor = GridBagConstraints.PAGE_END; //bottom of space
            c.insets = new Insets(5,0,0,0);  //top padding
            c.gridx = 1;       //aligned with text box 2
            c.gridwidth = 2;   //2 columns wide
            c.gridy = 2;       //third row
            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() {
            //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);
            
            //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) {
            //Set the theme
            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            }
            
            catch (Exception e) {
                System.out.println("Unable to load Windows look and feel");
            }
            //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
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You can set a fill constraint for each component in the pane. Take a look at the code sample contained in the GridBagLayout entry in the API documentation.

  3. #3
    Join Date
    Apr 2006
    Posts
    6
    Fixed just by setting c.weight = 1; on the text boxes.

  4. #4
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    I compiled ur code and noticed that your windows look and feel was not loading, so corrected the error, it may have been an error when you were copy and pasting your code.

    Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Main {
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        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;
            pane.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
            if (shouldFill) {
                //natural height, maximum width
                c.fill = GridBagConstraints.HORIZONTAL;
            }
            
            JLabel label1 = new JLabel("Text: ");
            if (shouldWeightX) {
                c.weightx = 0.5;
            }
            c.gridx = 0;
            c.gridy = 0;
            c.insets = new Insets(5,0,0,0);
            pane.add(label1, c);
            
            JTextField textbox1 = new JTextField();
            c.gridx = 1;
            c.gridy = 0;
            c.insets = new Insets(5,0,0,0);
            pane.add(textbox1, c);
            
            JLabel label2= new JLabel("Encrypted: ");
            if (shouldWeightX) {
                c.weightx = 0.5;
            }
            c.gridx = 0;
            c.gridy = 1;
            c.insets = new Insets(5,0,0,0);
            pane.add(label2, c);
            
            JTextField textbox2 = new JTextField();
            c.gridx = 1;
            c.gridy = 1;
            c.insets = new Insets(5,0,0,0);
            pane.add(textbox2, c);
            
            button = new JButton("Encrypt");
            c.ipady = 0;       //reset to default
            c.anchor = GridBagConstraints.PAGE_END; //bottom of space
            c.insets = new Insets(5,0,0,0);  //top padding
            c.gridx = 1;       //aligned with text box 2
            c.gridwidth = 2;   //2 columns wide
            c.gridy = 2;       //third row
            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() {
            //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);
            
            //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() {
    			//Set the theme
           	 	try {
                		UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            		}
            
            		catch (Exception e) {
                		System.out.println("Unable to load Windows look and feel");
            		}
                    createAndShowGUI();
                }
            });
        }
    }

Similar Threads

  1. Replies: 1
    Last Post: 11-23-2005, 05:35 AM
  2. GridBagLayout
    By David Lim in forum Java
    Replies: 0
    Last Post: 05-16-2003, 07:12 AM
  3. Display one record at a time!
    By ASP learner in forum ASP.NET
    Replies: 5
    Last Post: 10-08-2002, 08:17 PM
  4. question on best programming option
    By James in forum .NET
    Replies: 2
    Last Post: 03-28-2002, 08:14 AM
  5. Performance question
    By John in forum VB Classic
    Replies: 2
    Last Post: 04-05-2000, 10:04 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center