Search code examples
javaswinglayout-managergridbaglayout

Java function for building GUI panel


I'm building a GUI layout for my application using the grid bag and I'm trying to come up with a function to layout each element so that I don't have to keep re typing the same grid bag code over and over. I want to rewrite this code:

GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints bc = new GridBagConstraints();
this.setLayout(gridbag);

bc.fill = GridBagConstraints.HORIZONTAL;
bc.anchor = GridBagConstraints.WEST;
bc.insets = new Insets(0, 10, 10, 0);
bc.gridx = 0;
bc.gridy = 0;
bc.gridwidth = 1;
this.add(programNameLabel, bc);

so that it can be written calling a function like this:

labelPosition(GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 0, 10, 10, 0, 0, 0, 1, programNameLabel);

Here is the function that I have written for the task.

protected void labelPosition(int axis, int direction, int insetOne, int insetTwo, int insetThree, int insetFour, int gridX, int gridY, int gridWidth, JLabel name)
    {
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints bc = new GridBagConstraints();
        this.setLayout(gridbag);

        bc.fill = axis;
        bc.anchor = direction;
        bc.insets = new Insets(insetOne, insetTwo, insetThree, insetFour);
        bc.gridx = gridX;
        bc.gridy = gridY;
        bc.gridwidth = gridWidth;
        this.add(name, bc);
    }

Now it compiles, but when I run it, it doesn't work. All the labels are displayed in a single line rather than the layout that I'm looking for.

Is what I'm trying to do simply possible or am I missing something in my code?? Any suggestions?


Solution

  • You are creating a new GridBagLayout() each time you call your method. You should do that only once, and in your method only create the GridBagConstraints and add the new label to your container (and, btw, by using a more generic type like JComponent you can reuse the same method even for other components than JLabel):

    protected void addComponent(int axis, int direction, int insetOne, int insetTwo, int insetThree, int insetFour, 
                                int gridX, int gridY, int gridWidth, JComponent component) {
    
        GridBagConstraints bc = new GridBagConstraints();
    
        bc.fill = axis;
        bc.anchor = direction;
        bc.insets = new Insets(insetOne, insetTwo, insetThree, insetFour);
        bc.gridx = gridX;
        bc.gridy = gridY;
        bc.gridwidth = gridWidth;
    
        this.add(component, bc);
    }
    
    ...
    GridBagLayout gridbag = new GridBagLayout();
    this.setLayout(gridbag);
    
    addComponent(GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 0, 10, 10, 0, 0, 0, 1, new JLabel("Hello"));
    addComponent(GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST, 0, 10, 10, 0, 0, 1, 1, new JButton("World"));
    ...
    

    On a side note, if this is a new project, you might consider looking at JavaFX instead of Swing.