Reputation: 23
i have a problem with some buttons and panels. I created a JPanel with a GridBagLayout 6x1.
GraphicPaintedPanel deck = new GraphicPaintedPanel();
GridBagLayout gblDecks = new GridBagLayout();
gblDecks.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
gblDecks.rowHeights = new int[]{0, 0};
gblDecks.columnWeights = new double[]{0.15, 0.15, 0.15, 0.15, 0.15, 0.15, Double.MIN_VALUE};
gblDecks.rowWeights = new double[]{1.0, Double.MIN_VALUE};
deck.setLayout(gblDecks);
The problem comes when i try to add buttons to this panel as they take space based on how many they are, even tho they should have fixed size. For example if I do
for(int j=0; j<4; j++){
JButton activate= new JButton("ACTIVATE LEADER CARD");
GridBagConstraints gbcActivate = new GridBagConstraints();
gbcActivate.gridx = j;
gbcActivate.fill = GridBagConstraints.BOTH;
deck.add(activate, gbcActivate);
}
I create 4 buttons and the panel is supposed to have 4 button and 2 "empty spaces". But instead it ha 4 buttons that take all the space they can.
If i do
for(int j=0; j<6; j++){
JButton activate= new JButton("ACTIVATE LEADER CARD");
GridBagConstraints gbcActivate = new GridBagConstraints();
gbcActivate.gridx = j;
gbcActivate.fill = GridBagConstraints.BOTH;
deck.add(activate, gbcActivate);
}
Shouldn't the dimensions be fixed? Can anyone help me?
Upvotes: 0
Views: 354
Reputation: 11143
gbcActivate.fill = GridBagConstraints.BOTH
If we go to the tutorial it says:
fill
Used when the component's display area is larger than the component's requested size to determine whether and how to resize the component. Valid values (defined as GridBagConstraints constants) include NONE (the default), HORIZONTAL (make the component wide enough to fill its display area horizontally, but do not change its height), VERTICAL (make the component tall enough to fill its display area vertically, but do not change its width), and BOTH (make the component fill its display area entirely).
So, it will fill the display area independtly of what you specify for the columnWeights
, etc. Try removing it or changing it for GridBagConstraints.NONE
Or as suggested in the comments above, use another layout manager such as FlowLayout
Upvotes: 1