I am designing a very basic form in which I am using GridBagLayout to position and size the GUI components. All the components get sized and positioned exactly as I expect from my code except the two radio buttons named "noRadio" and "yesRadio" respectively. I want them to appear next to the label with text "Family Inherited". Instead, the yesRadio appears inside the JTextField next to the "Patient ID" label and the noRadio simply doesn't appear. Please see the attached screenshot for more details.
Here is the code of my form class that is extended from JPanel.
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
/**
*
* @author Muhammad Anas
*/
public class UpdatePatientForm extends JPanel
{
private JLabel patientIdLabel;
private JTextField patientIdField;
private JLabel patientNameLabel;
private JTextField patientNameField;
private JLabel ageLabel;
private JTextField ageField;
private JLabel yearsLabel;
private JLabel sexLabel;
private JComboBox sexCombo;
private JComboBox diseaseTypeCombo;
private JLabel diseaseTypeLabel;
private JTextField durationField;
private JLabel durationLabel;
private JComboBox durationUnitCombo;
private ButtonGroup familyInheritedGroup;
private JLabel familyInheritedLabel;
private JRadioButton noRadio;
private JRadioButton yesRadio;
private JLabel historyLabel;
private JTextArea historyArea;
private JScrollPane historyScrollPane;
private JLabel medicineLabel;
private JTextArea medicineArea;
private JScrollPane medicineScrollPane;
private JButton resetRecordButton;
private JButton updateRecordButton;
private GridBagLayout layout;
private GridBagConstraints constraints;
public UpdatePatientForm()
{
patientIdLabel = new JLabel( "Patient ID ");
patientIdField = new JTextField();
patientNameLabel = new JLabel( "Name" );
patientNameField = new JTextField();
ageLabel = new JLabel( "Age" );
ageField = new JTextField();
yearsLabel = new JLabel( "Years" );
sexLabel = new JLabel( "Sex" );
String [] sexOptions = { "Select", "Male", "Female" };
sexCombo = new JComboBox( sexOptions );
String [] diseaseTypes = { "Select", "Fever", "Flue", "Blood Pressure", "Hepatitis", "Cancer", "Diabetes", "Haemophilia" };
diseaseTypeCombo = new JComboBox( diseaseTypes );
diseaseTypeLabel = new JLabel( "Disease Type" );
durationField = new JTextField();
durationLabel = new JLabel( "Disease Duration" );
String [] durationUnits = { "Select", "Days", "Weeks", "Months", "years" };
durationUnitCombo = new JComboBox( durationUnits );
familyInheritedGroup = new ButtonGroup();
familyInheritedLabel = new JLabel( "Family Inherited" );
noRadio = new JRadioButton( "No" );
yesRadio = new JRadioButton( "Yes" );
historyLabel = new JLabel( "Patient History" );
historyArea = new JTextArea();
historyScrollPane = new JScrollPane( historyArea );
medicineLabel = new JLabel( "Medicine Proposed" );
medicineArea = new JTextArea();
medicineScrollPane = new JScrollPane( medicineArea );
resetRecordButton = new JButton( "Reset" );
updateRecordButton = new JButton( "Update" );
////////////// --------- components initialization done here ------------ \\\\\\\\\\\
layout = new GridBagLayout();
constraints = new GridBagConstraints();
setLayout( layout );
constraints.anchor = GridBagConstraints.WEST;
constraints.fill = GridBagConstraints.BOTH;
constraints.weighty = 0;
constraints.weightx = 1;
addComponent( patientIdLabel, 0, 0, 1, 2 );
addComponent( patientNameLabel, 1, 0, 1, 2 );
addComponent( durationLabel, 2, 0, 1, 2 );
addComponent( familyInheritedLabel, 3, 0, 1, 2 );
// following two lines are the code to add those two
// radio boxes which don't appear at the place where I expect them to
addComponent( yesRadio, 3, 2, 1, 1 );
addComponent( yesRadio, 3, 3, 1, 1 );
////////////////////////////////////////////////////////////////////////
addComponent( historyLabel, 4, 0, 1, 3 );
addComponent( medicineLabel, 8, 0, 1, 3 );
addComponent( sexLabel, 1, 6, 1, 2 );
addComponent( diseaseTypeLabel, 2, 6, 1, 2 );
addComponent( ageLabel, 3, 6, 1, 2 );
constraints.weightx = 0; // the label "years" should not even resize horizontally
addComponent( yearsLabel, 3, 10, 1, 1 );
constraints.weightx = 1; // rest of the components should however resize horizontally
constraints.anchor = GridBagConstraints.CENTER;
addComponent( patientIdField, 0, 2, 1, 3 );
addComponent( patientNameField, 1, 2, 1, 3 );
addComponent( durationField, 2, 2, 1, 1 );
addComponent( durationUnitCombo, 2, 3, 1, 2 );
addComponent( sexCombo, 1, 8, 1, 3 );
addComponent( diseaseTypeCombo, 2, 8, 1, 3);
addComponent( ageField, 3, 8, 1, 2 );
constraints.weightx = 2;
constraints.weighty = 1;
addComponent( historyScrollPane, 5, 0, 3, GridBagConstraints.REMAINDER );
addComponent( medicineScrollPane, 9, 0, 3, GridBagConstraints.REMAINDER );
}
// a utility function
// add a Component to the panel 'row' row, 'col' column and it should
// occupy 'rows' rows and 'columns' columns
private void addComponent( Component component, int row, int col, int rows, int columns )
{
constraints.gridx = col;
constraints.gridy = row;
constraints.gridheight = rows;
constraints.gridwidth = columns;
layout.setConstraints( component, constraints);
add( component );
}
}
and here is the code of the driver class that creates an object of JFrame and adds an instance of UpdatePatientForm to it.
import javax.swing.JFrame;
public class Client
{
public static void main( String [] args )
{
JFrame app = new JFrame();
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
app.add( new UpdatePatientForm() );
app.setSize( 550, 600 );
app.setVisible( true );
}
}
On running this program, I get this window!
Please give me some hints about what could be a reason of this problem?