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

Recommended Answers

All 3 Replies

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

commented: I didn't realize object had to be changed to component. Thanks for the help bud +1

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

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

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.