Well I'm going through a lot of my code and wanting to clean it up a bit by creating functions to do the necessary tasks instead of having code that is multiple lines long.

For example, I use the following code multiple times

ViewObject vo = new ViewObject(a, b, c, d, e, f);

        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = row;
        gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;

        jPanel.add(vo, gridBagConstraints);

So if I wanted to create a function to take care of this but be able to change the viewobject, gridy, and panel what would the function look like?

I know the parameters would be something like:

public void constraints(int row, Object object, JPanel panel){

    }

Basically I'm just looking for how to make gridy = row, vo = object, jPanel = panel and my mind seems to be coming up with a blank

Just copy everything from lines 3-10 into your method and change the variables to use your method parameters.

Comments
I didn't realize object had to be changed to component. Thanks for the help bud

Just copy everything from lines 3-10 into your method and change the variables to use your method parameters.

Thats what I had tried originally, but it gives me a problem where the viewobject is

panel.add(object, gridBagConstraints);

It wants me to cast object to component, would there be a problem doing this? Or should I just change my parameter to a Component object instead of Object object

Edited 7 Years Ago by KirkPatrick: n/a

You may as well change the method parameter to Component, since anything you would want to add like that would extend Component.

This question has already been answered. Start a new discussion instead.