javaswinglayout-managergridbaglayoutinsets

Problem with GridBagLayout while adding JComboBox and JButton


In the output, I don't need the gap between the button 4 (b4) and button 5 (b5) but I couldn't do that and I tried insets also but my attempts are in vain. Can you help me out with any suggestions or any mistakes I have committed?

I observed that this problem is arising when I add both JComboBox and JButton.

Even I could not set insets or boundaries through setBounds() or Inset class.

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

public class FontWindow {
    FontWindow() {
        JFrame f = new JFrame();
        JPanel p = new JPanel();
        p.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        JComboBox c1,c2;
        String clist1[] = {"Courier New","Times New Roman","Calibri"};
        String clist2[] = {"8","9","10","11","12","14","16","18","20","22","24","26","28","36","72"};
        c1 = new JComboBox(clist1);
        c2 = new JComboBox(clist2);

        gbc.gridx = 0;
            gbc.gridy = 0;
            p.add(c1,gbc);

        gbc.gridx = 1;
            gbc.gridy = 0;
            p.add(c2,gbc);

        JButton b1,b2,b3;
        b1 = new JButton("B1");
        b2 = new JButton("B2");
        b3 = new JButton("B3");

        //gbc.insets = new Insets(0,5,0,0);
        gbc.gridx = 2;
            gbc.gridy = 0;
            p.add(b1,gbc);

        gbc.gridx = 3;
            gbc.gridy = 0;
            p.add(b2,gbc);

        gbc.gridx = 4;
            gbc.gridy = 0;
            p.add(b3,gbc);

        JButton b4,b5,b6;

        b4 = new JButton("B4");
        b5 = new JButton("B5");
        b6 = new JButton("B6");

        //gbc.insets = new Insets(0,10,0,5);
        gbc.anchor = GridBagConstraints.BELOW_BASELINE_LEADING;
        gbc.gridx = 0;
            gbc.gridy = 1;
            p.add(b4,gbc);
        gbc.gridx = 1;
            gbc.gridy = 1;
            p.add(b5,gbc);
        gbc.gridx = 2;
            gbc.gridy = 1;
            p.add(b6,gbc);

        f.setSize(600,400);
        f.add(p);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
    public static void main(String args[]) {
        new FontWindow();
    }
}

This is the output I got:


Solution

  • Welcome to SO. See comments to follow the changes in the constrains:

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 3; //c1 is wide so let it span over 3 columns
        p.add(c1,gbc);
    
        gbc.gridwidth = 1;//reset span to 1
        gbc.gridx = 3;    //amend column numbers of row 0
        gbc.gridy = 0;
        p.add(c2,gbc);
    
        JButton b1,b2,b3;
        b1 = new JButton("B1");
        b2 = new JButton("B2");
        b3 = new JButton("B3");
    
        gbc.gridx = 4;
        gbc.gridy = 0;
        p.add(b1,gbc);
    
        gbc.gridx = 5;
        gbc.gridy = 0;
        p.add(b2,gbc);
    
        gbc.gridx = 6;
        gbc.gridy = 0;
        p.add(b3,gbc);
    
        //no change at the rest of the code 
    

    You could get better control using simpler to handle layout managers by dividing the layout to smaller, easier to manage JPanels.
    For example wrapping each components row with a JPanel:

        JPanel p = new JPanel();
        p.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
    
        JPanel topRowPane = new JPanel();//uses FlowLayout by default
        gbc.gridx = 0;
        gbc.gridy = 0;
        p.add(topRowPane,gbc);
    
        String clist1[] = {"Courier New","Times New Roman","Calibri"};
        String clist2[] = {"8","9","10","11","12","14","16","18","20","22","24","26","28","36","72"};
        JComboBox<String> c1 = new JComboBox<>(clist1);
        JComboBox<String> c2 = new JComboBox<>(clist2);
    
        topRowPane.add(c1);
        topRowPane.add(c2);
    
        JButton b1 = new JButton("B1");
        JButton b2 = new JButton("B2");
        JButton b3 = new JButton("B3");
        topRowPane.add(b1);
        topRowPane.add(b2);
        topRowPane.add(b3);
    
        JPanel bottomRowPane = new JPanel();
        gbc.anchor = GridBagConstraints.LINE_START;
        gbc.gridx = 0;
        gbc.gridy = 1;
        p.add(bottomRowPane,gbc);
    
        JButton b4 = new JButton("B4");
        JButton b5 = new JButton("B5");
        JButton b6 = new JButton("B6");
        bottomRowPane.add(b4);
        bottomRowPane.add(b5);
        bottomRowPane.add(b6);