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.