CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2001
    Location
    Madrid-Spain
    Posts
    1,123

    Problems with GridBagLayout

    I have a frame sized 640x480, which I want to divide horizontally into 2 parts, one taking 1/3 of the total width and the other the rest. The former section will contain some buttons, the latter some TextFields.
    I've been trying GridBagLayout and BoxLayout but I am not able to get the 1/3 - 2/3 division.
    I am Miss Maiden... Miss Iron Maiden :-D

  2. #2
    Join Date
    Apr 2004
    Posts
    131

    Re: Problems with GridBagLayout

    Originally posted by irona20
    I have a frame sized 640x480, which I want to divide horizontally into 2 parts, one taking 1/3 of the total width and the other the rest. The former section will contain some buttons, the latter some TextFields.
    I've been trying GridBagLayout and BoxLayout but I am not able to get the 1/3 - 2/3 division.
    Well, your problem is that you need to set the weight of the GridBagConstraints. The left side should have a weight of 1, the right side should have a weight of 2.

    If your main class extends JFrame, you can do this by saying:
    Code:
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    getContentPane().setLayout(gbl);
    
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    addLeftElements();
    
    gbc.weightx = 2.0;
    gbc.weighty = 2.0;
    addRightElements();
    This will divide it up appropriately. If you don't know how to add elements and apply them to constraints, then say so and I'll tell you.

    (when specifiying a weight, every item you add after specifying a weight will have that weight. So if you want to change the weight for every item, you'll have to say those two lines of code before you add every item)

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    i disagree with the Y weightings.. it may cause distortion of the display if the panel ever becomes more than one row of components high.

    the X weightings are correct, however.

    You should read the gridbag tutorial in my signature, for information about how to effectively size and weight your components, as weighting only affects how components grow. if, in the original layout, they have identical preferred sizes, they will appear to be 50/50 split. if 100% new pixels become availableone will get 33% of the new ones, and the other will get 66% of the new ones, but their overall sizes will be 42% 58% split (50+33)/2 (50+66)/2

    like i say.. read the tut, it details what effect the weights have, and what significance the minimum, preferred and max sizes have
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Re: Problems with GridBagLayout

    Originally posted by Demonpants Software

    (when specifiying a weight, every item you add after specifying a weight will have that weight. So if you want to change the weight for every item, you'll have to say those two lines of code before you add every item)
    only if you reuse the gbc

    i think my netbeans makes a new one each time...
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  5. #5
    Join Date
    Apr 2004
    Posts
    131
    Originally posted by cjard
    i disagree with the Y weightings.. it may cause distortion of the display if the panel ever becomes more than one row of components high.
    He did say he only wanted width to change, but regardless, you have to specify a new Y weight if you want the height to change at all when the window is resized. So, if you don't like that 2 to 1 ratio I gave, instead just say:

    gbc.weighty = 1.0;

    for both of them so that the y will resize at all.

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    I made a simple JFrame with 2 Panels.. dont ask why it's called ThreeButs :)

    If you observe, the panels have min sizes of 10x10, preferred sizes are different; 213x480 for the 1/3 one and 427x480 for the 2/3 one. this causes them to appear already 33:67 split when the frame is pack()ed.

    After that the weightx of 1.0 on the 1/3 panel and weightx of 2 on the 2/3 panel means that the total weight for the row is 1+2 = 3, so the 1/3 panel gets 1/total = 1/3 of any new pixels that become available (or less, if the window shrinks by 100 pix, the panels will shrink by 33/67 respectively) and the 2/3 panel gets 2/total = 2/3 of any new pixels available..

    run the program and you will see a yellow and blue rectangle pair that maintain 33:67 split :)

    Code:
    /*
     * ThreeButs.java
     *
     * Created on 05 May 2004, 19:12
     */
    
    /**
     *
     * @author  admin
     */
    public class ThreeButs extends javax.swing.JFrame {
      
      /** Creates new form ThreeButs */
      public ThreeButs() {
        initComponents();
      }
      
      /** This method is called from within the constructor to
       * initialize the form.
       * WARNING: Do NOT modify this code. The content of this method is
       * always regenerated by the Form Editor.
       */
      private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
    
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
    
        getContentPane().setLayout(new java.awt.GridBagLayout());
    
        setFont(new java.awt.Font("Arial", 0, 10));
        addWindowListener(new java.awt.event.WindowAdapter() {
          public void windowClosing(java.awt.event.WindowEvent evt) {
            exitForm(evt);
          }
        });
    
        jPanel1.setBackground(new java.awt.Color(255, 255, 51));
        jPanel1.setPreferredSize(new java.awt.Dimension(213, 480));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jPanel1, gridBagConstraints);
    
        jPanel2.setBackground(new java.awt.Color(0, 51, 255));
        jPanel2.setPreferredSize(new java.awt.Dimension(427, 480));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 2.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jPanel2, gridBagConstraints);
    
        pack();
      }
      
      /** Exit the Application */
      private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
      }
      
      /**
       * @param args the command line arguments
       */
      public static void main(String args[]) {
        new ThreeButs().show();
      }
      
      
      // Variables declaration - do not modify
      private javax.swing.JPanel jPanel1;
      private javax.swing.JPanel jPanel2;
      // End of variables declaration
      
    }
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  7. #7
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    Originally posted by Demonpants Software
    you have to specify a new Y weight
    not strictly true.. remember that GridBag is based on an invisible grid and the weightx and weighty cause the grid to grow and shrink. whether a component changes size or not depends on its fill property.. so you could set the weighty to zero for one of the panels, but the weighty of 1.0 on the other panel will cause the row to grow, and so long as the component with weighty 0 has a fill of BOTH, it will still grow and shrink in step.. try it in the example app i posted

    additionally.. you often reuse your gbc, so if you set the weighty of 1, then added the first panel, you wouldnt need to set it to something else when you add your second panel, because weighty is already 1.. it doesnt need to be a different value, and indeed, choosing different values can be very confusing for the layout manager (or you, whichever way you look at it) because you may end up with the situation where the layout manager is using values from one weighted row/col to resize the display(it uses the heaviest row and column) and you think it is using another..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  8. #8
    Join Date
    Apr 2004
    Posts
    131
    Ack! Cant you import some things into that program above? All the javax.swing.thingy's are making me dizzy!

  9. #9
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    i dont actually import anything, unless im throwing together a quick test app for, e.g. reading a line of text from a file and writing to a socket.. then i import all of java.io ..

    not importing makes a program start faster, plus I can see what is what.. if you have a good editor that syntax highlights the class name (UltraEdit shows like this: javax.swing.GridBagConstraints) then you tend not to even notice the package names.. but they do come in handy to see all the time, cause you can then tell where things are coming from, and it helps you remember where they are

    lol.. basically, i just try to make java as different from VB as possible..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  10. #10
    Join Date
    May 2001
    Location
    Madrid-Spain
    Posts
    1,123
    Wow, I've read your Gridbaglayout tutorial and it's fantastic. It will help me a lot. Thank you very much for your help
    I am Miss Maiden... Miss Iron Maiden :-D

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured