1

I was recently introduced to GridBagLayout to substitute the vanilla JPanel layout, but I am having trouble working with the functions like gridwidth/height and gridx/y. I am not sure how they work exactly in changing the size of the GUI and the positions of the buttons. Below is a code my professor gave me to use for reference, and I tried fiddling with certain numbers but the results never turned out to be what I expected to happen, and I am not sure why.

EDIT 1: To clarify, I am wondering exactly how do the gridwidth, gridheight, gridx, and gridy functions work to resize the GUI and position the Buttons' locations.

import java.awt.*;
import javax.swing.*;

class Bar1 {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        c.gridx=0;
        c.gridy=0;
        c.gridwidth=2;
        c.gridheight=1;
        c.weightx = 2;
        c.weighty = 1;
        c.fill = GridBagConstraints.BOTH;
        JButton b = new JButton("Hello");
        panel.add(b, c);
        c.gridx=0;
        c.gridy=1;
        c.gridwidth=1;
        c.gridheight=1;
        JButton b2 = new JButton("World");
        panel.add(b2, c);
        c.gridx=1;
        c.gridy=1;
        c.gridwidth=1;
        c.gridheight=1;
        JButton b3 = new JButton("!!!");
        panel.add(b3, c);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
David L
  • 47
  • 1
  • 7
  • 2
    `but the results never turned out to be what I expected` - and we are not mind readers. We have no idea what you expect to happen. Read the section from the Swing tutorial on [How to use GridBagLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) for an explanation of all the constraints and a working example. – camickr Apr 23 '15 at 00:00
  • @camickr I did not say my expectations because I do not want an answer that tells me the code I need to only do what I need, I was hoping someone could tell me, like I asked in the question title, how do the gridwidth/height gridx/y functions work to resize the GUI and buttons. – David L Apr 23 '15 at 00:07
  • 2
    And you were given the answer. The tutorial explains how the constraints work. You were also given a link to the tutorial in your last question (http://stackoverflow.com/a/29786937/131872). So read the tutorial. If there is something you don't understand from the tutorial then ask a specific question. We don't know what your code is expected to do and we don't know what you find confusing from the tutorial. – camickr Apr 23 '15 at 00:27
  • 1
    `gridwidth` and `gridheight` determine the number of cells a component can occupy. `gridx` and `gridy` determine the cell that the component starts in – MadProgrammer Apr 23 '15 at 00:34
  • 1
    How is that different than the explanation in the tutorial? `To clarify, I am wondering exactly how do the gridwidth, gridheight, gridx, and gridy functions work to resize the GUI and position the Buttons' locations.` - what part of the wording in the tutorial do you not understand? You specified one component in the first row that occupies 2 cells and 2 components in the second row that each occupy one cell. What don't you understand about your code? – camickr Apr 23 '15 at 00:43
  • @camickr Look, I am sorry if this frustrated you because you thought I was too lazy to read the guide. I get the feeling that there is alot of people that come to this site and are too lazy to read already made guides. But I did read that guide posted on my previous question, over and over. I just couldn't grasp it. I am not sure why, that's why I came to ask on this site, for a simplified answer. Next time I ask a question I will try to be more specific though. – David L Apr 23 '15 at 00:53
  • 1
    @DavidL, `... too lazy to read already made guides` - exactly. So prove to us that you did read the tutorial when you have been given a link. `I just couldn't grasp it.` - So quote the sentence that you don't understand, then maybe we can expand on it. We can't guess what wording is confusing you. I still don't see the difference between the tutorial explanation and Mad's explanation, especially when you look at the code you posted and the layout that resulted. What did you expect to happen differently? – camickr Apr 23 '15 at 00:58
  • 1
    Also, don't forget to "accept" the answer you received in your last question to indicate that a solution has been found. – camickr Apr 23 '15 at 01:00
  • @camickr Thank you, I just did that. Also if you really want me to prove I'm not just some lazy leech, when they bring up "GridBagConstraints.RELATIVE", on the gridx, gridy section, it just trips me up because I'm not sure how to use that. I try to imagine how to implement it into the code, how it would fit but I'm just no sure of the way that would look correct. From the first two sentences of the gridx, gridy section I got that by default the upper left corner was the starting point and that its coordinates were 0, 0. What I didn't understand was whether using that code meant changing where – David L Apr 23 '15 at 03:39
  • the object I was trying to place would be? or would that be like changing the whole coordinate plane like in python, where the center of the screen is 0, 0. Its just once I read something I didn't understand I would constantly try to make sense of it and if I tried to move on without fully understanding how to use that command I would just not be able to understand the rest of that section. With the getwidth, getheight, I thought that meant the number of columns and rows for the whole GUI because it said "component's display area". Cut me some slack, I'm just starting out in this field. – David L Apr 23 '15 at 03:43
  • `"GridBagConstraints.RELATIVE", on the gridx, gridy section, it just trips me up because I'm not sure how to use that. I try to imagine how to implement it into the code` - agreed is it confusing, that is why you ask a specific question about what is concerning you. I still haven't mastered GridBagLayout but I need specific questions in order to attempt to help. You wrote code that did something. Either it did what you wanted (in which case you understand the constraints better than you think) or it didn't do what you wanted, in which case you ask a specific question. – camickr Apr 23 '15 at 04:37

0 Answers0