3

Please have a look at the following code

package normal;

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

public class Form extends JFrame
{
    private JLabel heightLabel, weightLabel, waistLabel, neckLabel, hipsLabel,genderLabel,valuesLabel,bfPercentageLabel;
    private JLabel logoLabel; 

    private ImageIcon logo;

    private JTextField heightTxt, weightTxt, waistTxt, neckTxt, hipsTxt;

    private JRadioButton maleRadio, femaleRadio, inchesRadio, cmRadio;
    private ButtonGroup genderGroup, valuesGroup;

    private JComboBox percentageCombo;

    private JPanel centerPanel, northPanel, southPanel;


    public Form()
    {
        //Declaring instance variables  
        heightLabel = new JLabel("Height: ");
        weightLabel = new JLabel("Weight: ");
        waistLabel = new JLabel("Waist: ");
        neckLabel = new JLabel("Neck: ");
        hipsLabel = new JLabel("Hips: ");
        genderLabel = new JLabel("Gender: ");
        valuesLabel = new JLabel("Values in: ");


        logoLabel = new JLabel();
        logo = new ImageIcon(getClass().getResource("/images/calc_logo_final_2_edit.gif"));
        logoLabel.setIcon(logo);



        heightTxt = new JTextField(10);
        weightTxt = new JTextField(10);
        waistTxt = new JTextField(10);
        neckTxt = new JTextField(10);
        hipsTxt = new JTextField(10);

        maleRadio = new JRadioButton("Male");
        femaleRadio = new JRadioButton("Female");
        genderGroup = new ButtonGroup();
        genderGroup.add(maleRadio);
        genderGroup.add(femaleRadio);

        inchesRadio = new JRadioButton("Inches");
        cmRadio = new JRadioButton("Centimeters");
        valuesGroup = new ButtonGroup();
        valuesGroup.add(inchesRadio);
        valuesGroup.add(cmRadio);

        percentageCombo = new JComboBox();
        percentageCombo.addItem("No Value is Set");

        this.add(createNorthPanel(),"North");
        this.add(createCenterPanel(),"Center");
        this.setResizable(false);
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    private JPanel createNorthPanel()
    {
        northPanel = new JPanel();
        northPanel.setLayout(new FlowLayout());

        northPanel.add(logoLabel);

        return northPanel;
    }

    private JPanel createCenterPanel()
    {
        centerPanel = new JPanel();

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();

        centerPanel.setLayout(gbl);

        //creating a jpanel for gender radio buttons
        JPanel genderPanel = new JPanel();
        genderPanel.setLayout(new FlowLayout());
        genderPanel.add(genderLabel);
        genderPanel.add(maleRadio);
        genderPanel.add(femaleRadio);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(heightLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(heightTxt,gbc);

        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(weightLabel,gbc);

        gbc.gridx = 4;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(weightTxt,gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(waistLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(waistTxt,gbc);

        gbc.gridx = 3;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(neckLabel,gbc);

        gbc.gridx = 4;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(neckTxt,gbc);

        gbc.gridx = 5;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(hipsLabel,gbc);

        gbc.gridx = 6;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(hipsTxt,gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(genderLabel,gbc);

        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,5,0,0);
        centerPanel.add(maleRadio,gbc);

        gbc.gridx = 3;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15,-10,0,0);
        centerPanel.add(femaleRadio,gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(50,5,0,0);
        centerPanel.add(valuesLabel,gbc);



        return centerPanel;


    }
}

As you can see, the JRadio button "female" is hiding a part of it, and once you move your cursor, it shows up completely. I guess this is happening because it is using minus spacing in "insets".

However I did it like that to reduce the gap between 2 radio buttons. Male is in gridx = 2, and female is in gridx = 3, which is a massive space between the buttons.So I used minus space in insets to reduce the space, unfortunately it came like this.

I tried adding the JLabel, maleRadio and femaleRadio into a seperate JPanel which is having flowlayout, and put it into gbc.gridx = 2; gbc.gridy = 3;. It made everything worst by matching all the cells in gridy = 3 into the width of new JPanel.

Please help me to reduce the gap between these 2 JRadio buttons, without having any issue. Thank you.

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • 2
    `Please have a look at the following code`-- Your code is too long and unwieldy to look at a glance, for better help sooner post an [SSCCE](http://sscce.org/) – Extreme Coders Jan 06 '13 at 16:01
  • 1
    `As you can see, the JRadio button`-- we cannot see anything , post a screenshot for the problem you are having. – Extreme Coders Jan 06 '13 at 16:03
  • 1
    @Sepala : What's the point of using the same GridBagConstraints's object, if you have to write the whole code again, try to skip values that you already had set, use only those that you need to apply anew – nIcE cOw Jan 06 '13 at 16:53

1 Answers1

4
  • Dont extend JFrame rather create an instance and use that.

  • Also I see you add your radio buttons to a panel but you dont add the panel rather you re-add the radio buttons to centerpanel? choose one way lose the other (though I think this might have occurred in trying to mend the problem?)

  • An SSCCE most importantly is compilable (via correct syntax and no compile error) and runnable (via a main method and no runtime exceptions (unless thats the problem :P) - like your reading of the image - please find a way to include resources i.e link to an URL with the logo or make a method return a simple image the same size as logo or simply leave it out).

The problem is here:

    gbc.gridx = 3;
    gbc.gridy = 3;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets = new Insets(15, -10, 0, 0);
    centerPanel.add(femaleRadio, gbc);

-10 should definitely not be there (maybe typo?) as this will cause it to overlap or in this case underlap another component, rather use anything greater than or equal to 0:

    gbc.gridx = 3;
    gbc.gridy = 3;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets = new Insets(15, 10, 0, 0);
    centerPanel.add(femaleRadio, gbc);

which would give us:

enter image description here

UPDATE:

Also it is important to note GridBagContsraints like gridx etc start at 0 and not 1.

+1 to @Gagandeeps balis comment on re-using values which have been set already and are the same in GridBagConstraints, here is your code with all talked about fixes:

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

class Form {

    private JLabel heightLabel, weightLabel, waistLabel, neckLabel, hipsLabel, genderLabel, valuesLabel, bfPercentageLabel;
    private JLabel logoLabel;
    private ImageIcon logo;
    private JTextField heightTxt, weightTxt, waistTxt, neckTxt, hipsTxt;
    private JRadioButton maleRadio, femaleRadio, inchesRadio, cmRadio;
    private ButtonGroup genderGroup, valuesGroup;
    private JComboBox percentageCombo;
    private JPanel centerPanel, northPanel, southPanel;

    public Form() {
        //Declaring instance variables  
        heightLabel = new JLabel("Height: ");
        weightLabel = new JLabel("Weight: ");
        waistLabel = new JLabel("Waist: ");
        neckLabel = new JLabel("Neck: ");
        hipsLabel = new JLabel("Hips: ");
        genderLabel = new JLabel("Gender: ");
        valuesLabel = new JLabel("Values in: ");

        logoLabel = new JLabel();
        //logo = new ImageIcon(getClass().getResource("/images/calc_logo_final_2_edit.gif"));
        //logoLabel.setIcon(logo);

        heightTxt = new JTextField(10);
        weightTxt = new JTextField(10);
        waistTxt = new JTextField(10);
        neckTxt = new JTextField(10);
        hipsTxt = new JTextField(10);

        maleRadio = new JRadioButton("Male");
        femaleRadio = new JRadioButton("Female");
        genderGroup = new ButtonGroup();
        genderGroup.add(maleRadio);
        genderGroup.add(femaleRadio);

        inchesRadio = new JRadioButton("Inches");
        cmRadio = new JRadioButton("Centimeters");
        valuesGroup = new ButtonGroup();
        valuesGroup.add(inchesRadio);
        valuesGroup.add(cmRadio);

        percentageCombo = new JComboBox();
        percentageCombo.addItem("No Value is Set");

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createNorthPanel(), "North");
        frame.add(createCenterPanel(), "Center");
        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);


    }

    private JPanel createNorthPanel() {
        northPanel = new JPanel();
        northPanel.setLayout(new FlowLayout());
        northPanel.add(logoLabel);

        return northPanel;
    }

    private JPanel createCenterPanel() {
        centerPanel = new JPanel(new GridBagLayout());

        GridBagLayout gbl = new GridBagLayout();
        centerPanel.setLayout(gbl);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(15, 5, 0, 0);

        gbc.gridx = 0;
        gbc.gridy = 0;
        centerPanel.add(heightLabel, gbc);
        gbc.gridx = 1;
        centerPanel.add(heightTxt, gbc);
        gbc.gridx = 2;
        centerPanel.add(weightLabel, gbc);
        gbc.gridx = 3;
        centerPanel.add(weightTxt, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        centerPanel.add(waistLabel, gbc);
        gbc.gridx = 1;
        centerPanel.add(waistTxt, gbc);
        gbc.gridx = 2;
        centerPanel.add(neckLabel, gbc);
        gbc.gridx = 3;
        centerPanel.add(neckTxt, gbc);
        gbc.gridx = 4;
        centerPanel.add(hipsLabel, gbc);
        gbc.gridx = 5;
        centerPanel.add(hipsTxt, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        centerPanel.add(genderLabel, gbc);
        gbc.gridx = 1;
        centerPanel.add(maleRadio, gbc);
        gbc.gridx = 2;
        centerPanel.add(femaleRadio, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(50, 5, 0, 0);
        centerPanel.add(valuesLabel, gbc);

        return centerPanel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Form();
            }
        });
    }
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • hehe stumbled upon the problem as broswing the code, at first I was stumped though :P – David Kroukamp Jan 06 '13 at 16:48
  • 1
    I just got this thingy on my side too, just now, thought of positing this as a comment, but found you had already answered :-) – nIcE cOw Jan 06 '13 at 16:53
  • @DavidKroukamp: Thanks for the reply David. But this is the case I am talking about. See, the gap between male and female radio buttons are too much. I used minus values because of that, to reduce the gap. – PeakGen Jan 06 '13 at 17:15
  • @Sepala tried just setting them all to 0? Im talking about the label and 2 radio buttos in question? thats the best smallest the gap will get without using negative an overlapping components using current `LayoutManager` (not that Im a GridBag guru :)). Maybe try nesting `LayoutManager`s to achieve your desired look. Also take a look at my update. – David Kroukamp Jan 06 '13 at 18:18
  • @Sepala : Try this [**JRadioButton.setMargin()**](http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setMargin(java.awt.Insets)), hopefully this might be able to help. – nIcE cOw Jan 07 '13 at 06:06