Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: gridbaglayout does not resize with the window

  1. #1
    Marcoita_88 is offline Member
    Join Date
    Aug 2011
    Location
    Italy
    Posts
    9
    Rep Power
    0

    Default gridbaglayout does not resize with the window

    Hello, I have created a jFrame wiith a gridbaglayout, but when I resize the window the child components stay at center of the window. I want the components to resize with the window.
    This is the window when created.

    and this is the resized window.

    As you can see, the child components does NOT resize... I want they resize with the window.
    Can someone help me please?

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

  3. #3
    Marcoita_88 is offline Member
    Join Date
    Aug 2011
    Location
    Italy
    Posts
    9
    Rep Power
    0

    Default

    No, for every component weight x and y are set to 0.0.

    Here is the initComponents autogenerated code:

    Java Code:
    /** 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.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
            java.awt.GridBagConstraints gridBagConstraints;
    
            labelImmagine = new javax.swing.JLabel();
            labelNomeCompleto = new javax.swing.JLabel();
            labelStatoChat = new javax.swing.JLabel();
            taMioTesto = new javax.swing.JScrollPane();
            taInviaMessaggi = new javax.swing.JTextArea();
            jScrollPane1 = new javax.swing.JScrollPane();
            teResocontoMessaggi = new javax.swing.JEditorPane();
    
            setMinimumSize(new java.awt.Dimension(500, 200));
            getContentPane().setLayout(new java.awt.GridBagLayout());
    
            labelImmagine.setText("Immagine");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.gridheight = 3;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            gridBagConstraints.ipadx = 15;
            gridBagConstraints.ipady = 15;
            getContentPane().add(labelImmagine, gridBagConstraints);
    
            labelNomeCompleto.setBackground(new java.awt.Color(51, 255, 102));
            labelNomeCompleto.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            labelNomeCompleto.setText("jLabel1");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
            gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 166);
            getContentPane().add(labelNomeCompleto, gridBagConstraints);
    
            labelStatoChat.setText("jLabel3");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 2;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
            gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 166);
            getContentPane().add(labelStatoChat, gridBagConstraints);
    
            taInviaMessaggi.setColumns(20);
            taInviaMessaggi.setRows(3);
            taInviaMessaggi.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {
                    taInviaMessaggiKeyPressed(evt);
                }
                public void keyReleased(java.awt.event.KeyEvent evt) {
                    taInviaMessaggiKeyReleased(evt);
                }
            });
            taMioTesto.setViewportView(taInviaMessaggi);
    
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 4;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            gridBagConstraints.anchor = java.awt.GridBagConstraints.LAST_LINE_END;
            getContentPane().add(taMioTesto, gridBagConstraints);
    
            jScrollPane1.setMinimumSize(new java.awt.Dimension(80, 100));
            jScrollPane1.setPreferredSize(new java.awt.Dimension(80, 100));
    
            teResocontoMessaggi.setContentType("text/html");
            teResocontoMessaggi.setEditable(false);
            teResocontoMessaggi.setText("<html>\r\n  <head>\r\n\r    <LINK href=\"http://unknownsite.org/stileChat.css\" rel=\"stylesheet\" type=\"text/css\">\n  </head>\r\n  <body>\r</body>\r\n</html>\r\n");
            jScrollPane1.setViewportView(teResocontoMessaggi);
    
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 3;
            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
            gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
            getContentPane().add(jScrollPane1, gridBagConstraints);
    
            pack();
        }// </editor-fold>
    Last edited by Marcoita_88; 08-05-2011 at 11:20 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    No, for every component weight x and y are set to 0.0.
    Well, that explains it

    db
    Fubarable likes this.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    See: How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    The section on "Specifying Constraints" will explain why you need weightx/weighty.

  6. #6
    Marcoita_88 is offline Member
    Join Date
    Aug 2011
    Location
    Italy
    Posts
    9
    Rep Power
    0

    Default

    Thanks to all, I've solved the problem!
    Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.
    Now the resizing behavior of the components is correct. Thanks again.

Similar Threads

  1. Replies: 3
    Last Post: 05-05-2010, 01:15 PM
  2. Problem resizing JPanel on window resize
    By Nyet in forum AWT / Swing
    Replies: 4
    Last Post: 11-27-2009, 03:13 AM
  3. Window resize icon...
    By pele in forum SWT / JFace
    Replies: 3
    Last Post: 06-09-2008, 08:31 AM
  4. Replies: 1
    Last Post: 06-06-2008, 07:55 AM
  5. Replies: 1
    Last Post: 02-16-2008, 11:43 PM

Tags for this Thread

Posting Permissions

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