Question

Take a look on that code:

    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.NORTHEAST;
    c.weightx = 1;
    c.weighty = 1;
    c.gridx = 0;
    c.gridy = 0;
    this.add(buttonMinus,c);

    c.anchor = GridBagConstraints.NORTH;
    c.gridx = 1;
    c.gridy = 0;
    this.add(labelTime,c);

    c.anchor = GridBagConstraints.NORTHWEST;
    c.gridx = 2;
    c.gridy = 0;
    this.add(buttonPlus,c);

    c.anchor = GridBagConstraints.SOUTHWEST;
    c.gridwidth = 2;
    c.gridx = 0;
    c.gridy = 1;
    this.add(labelCoor,c);

how it looks like now: now

how i want to be: need

ofc i want to not resize my buttons or labels. let them stay their default sizes.

I know i can do it by adding to the north of BorderLayout panel a new FlowLayout panel but i want to do it with one GridBagLayout

Was it helpful?

Solution

ofc i want to not resize my buttons or labels. let them stay their default sizes.

Then you should set the weightx = 0, weighty = 0 so that their respective row and col will not get extra space, which you are currently setting to 1. Also GridBagLayout generally respects preferred size, but we should specify both preferred size and minimum size as is explained in a use case of this question. Also take a look to specifying constraint section of official sources for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top