Height Buttons

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

genderPanel = new JPanel();
genderPanel.setLayout( new BoxLayout(genderPanel , BoxLayout.Y_AXIS));

Height Buttons

Your Ideal Weight

Here is the section of the code that deals with the buttons for height. The buttons are added to (1) heightPanel, which controls their graphic representation on screen, and added to (2) heightGroup, which controls their logical operation.

One button of the heightGroup is set to be choosen by default. As with the gender buttons, BoxLayout is used in the panel to put the label and buttons in a column.


public class IdealWeight extends JFrame 
{
  JRadioButton heightA;
  JRadioButton heightB;
  JRadioButton heightC;
  JRadioButton heightD;
  JRadioButton heightE;
  ButtonGroup  heightGroup;
  JPanel       heightPanel;

  public IdealWeight()  
  { 
    // height group
    heightA = new JRadioButton("60 to 64 inches",false);
    heightB = new JRadioButton("64 to 68 inches",false);
    heightC = new JRadioButton("68 to 72 inches",false);
    heightD = new JRadioButton("72 to 76 inches",true );
    heightE = new JRadioButton("76 to 80 inches",false);
  
    heightGroup = new ButtonGroup();
    heightGroup.add(  );
    heightGroup.add(  );
    heightGroup.add(  );
    heightGroup.add(  );
    heightGroup.add(  );
  
    heightPanel = new JPanel();
    heightPanel.setLayout( new BoxLayout(heightPanel,BoxLayout.Y_AXIS) );
    heightPanel.add( new JLabel("Your Height") );
    heightPanel.add(  );
    heightPanel.add(  );
    heightPanel.add(  );
    heightPanel.add(  );
    heightPanel.add(  );
    
    . . . . .
  }
}

QUESTION 5:

Add the buttons to the button group and to the panel.