For this, in the absence of a good SSCCE , I think that you have not provided any restrictions for filling, which is used for
Used when the display area of a component is larger than the size of the requested component to determine how and how to resize the component. Valid values (defined as GridBagConstraints constants) include NONE (default), HORIZONTAL (make the component wide enough to fill its horizontal display area, but not change its height), VERTICAL (make the component high enough to fill its display area by vertically, but do not change its width), and BOTH (make the component completely fill the display area).
So you should add something like this to GridBagConstraints
constraintsGridBag.fill = GridBagConstraints.HORIZONTAL;
This will only allow you to expand HORIZONTALLY not in both directions.
** EDIT: Regarding the added code **
Never specify setPreferredSize (...) for any component in Swing. Let the layout manager you use, take care of this. Remove all setPreferredSize (...) objects, allowing them to remain at their normal size when resized.
* EDIT 2: *
Code to tell you what I say:
import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class GridBagTest extends JFrame { private JPanel topPanel; private JPanel bottomPanel; public GridBagTest() { setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridwidth = GridBagConstraints.REMAINDER;
source share