Results 1 to 6 of 6
- 08-05-2011, 09:35 PM #1
Member
- Join Date
- Aug 2011
- Location
- Italy
- Posts
- 9
- Rep Power
- 0
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?
-
Have you set the GridBagConstraint weightx and weighty properties to non-zero values?
- 08-05-2011, 11:16 PM #3
Member
- Join Date
- Aug 2011
- Location
- Italy
- Posts
- 9
- Rep Power
- 0
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.
- 08-05-2011, 11:23 PM #4
Well, that explains itNo, for every component weight x and y are set to 0.0..gif)
db
- 08-06-2011, 01:27 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 08-06-2011, 10:31 AM #6
Member
- Join Date
- Aug 2011
- Location
- Italy
- Posts
- 9
- Rep Power
- 0
Thanks to all, I've solved the problem!
Now the resizing behavior of the components is correct. Thanks again.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.
Similar Threads
-
When I resize window color of rectangles is changed
By cselic in forum Java 2DReplies: 3Last Post: 05-05-2010, 01:15 PM -
Problem resizing JPanel on window resize
By Nyet in forum AWT / SwingReplies: 4Last Post: 11-27-2009, 03:13 AM -
Window resize icon...
By pele in forum SWT / JFaceReplies: 3Last Post: 06-09-2008, 08:31 AM -
How to resize the components with the change in the size of the window?
By shaunak kiwalkar in forum SWT / JFaceReplies: 1Last Post: 06-06-2008, 07:55 AM -
Problems with gridBaglayout when I resize the window
By Iyengar in forum AWT / SwingReplies: 1Last Post: 02-16-2008, 11:43 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks