I'm wondering if it is possible to control certain aspects in my function. All my it does is control how an object gets placed on my gui.

I'm wondering if there is a way to control GridBagConstraints.fill and GridBagConstraints.anchor

Function:

public void gridBagControl(Component ob, JPanel p, Insets in, int row, int w_y, int g_x, int fill) {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = g_x;
        gridBagConstraints.gridy = row;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = w_y;
        gridBagConstraints.anchor = GridBagConstraints.CENTER; //want to add CENTER as a param
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; //want to add HORIZONTAL as a param
        gridBagConstraints.insets = in;
        p.add(ob,gridBagConstraints);
    }

Just thought I would post and see if anyone knew how to get those to work with the function. They seem to be ints which is what I tried at first however encountered some issues.

Anyways, I'm sure this will be a thread that is solved quickly. In advance, thanks for the help.

What issues did you encounter? Yes, they are just ints and you should be able to pass them just like you have in the method signature you posted.

When I take that function and add int params it tells me it can't find that symbol. For instance:

Function:

public void gridBagControl(Component ob, JPanel p, Insets in, int row, int w_y, int g_x, int fill, int c, int h) {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = g_x;
        gridBagConstraints.gridy = row;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = w_y;
        gridBagConstraints.anchor = GridBagConstraints.c; //can't find symbol
        gridBagConstraints.fill = GridBagConstraints.h; //can't find symbol
        gridBagConstraints.insets = in;
        p.add(ob,gridBagConstraints);
    }

My anchor will always be either CENTER or NONE
My fill will always be HORIZONTAL or NONE

That's because the entire expression GridBagConstraints.CENTER is the int constant value. Use the parameter directly

gridBagConstraints.anchor = c;

Oh gosh, sometimes I wonder what's going on in my head when I code.

Thank you for pointing that out Ezzaral.


So I would have to create a few extra ints for this then:

int aaa = GridBagConstraints.CENTER;
int bbb = GridBagConstraints.NONE;
int ccc = GridBagConstraints.HORIZONTAL;

then I could go ahead and use these in my function.

No, just use the constant itself when you call the method

gridBagControl(comp, p, in, 1, 1, x, GridBagConstraints.NONE, GridBagConstraints.CENTER)

Thank you, my code looks a bit cleaner now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.