Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with radio buttons and results.

  1. #1
    Junior Member Bewitched1's Avatar
    Join Date
    Jul 2010
    Location
    Simpsonville SC
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with radio buttons and results.

    I need help completing this Ideal weight calculator where the formula is
    Use the approximate formula:

    W = H2 / 30 , for female

    W = H2 / 28 , for male
    where W is the ideal weight in pounds, H is the height in inches. The ideal weight will display in the JLabel text box. I can not figure out where to input the action listeners to correspond with the formula. The code compiles but the buttons do not display the ideal weight.
    My code so far is:

    import java.awt.* ;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Assignment4_Cheshire extends JFrame
    {
      JRadioButton genderM, genderF;
      ButtonGroup  genderGroup;
      JPanel       genderPanel;
     
      JRadioButton heightA, heightB, heightC, heightD, heightE;
      ButtonGroup  heightGroup;
      JPanel       heightPanel;
     
      JTextField   resultText;
      JLabel       resultLabl;
      JPanel       resultPanel;
     
      public Assignment4_Cheshire()
      {
        setTitle( "Your Ideal Weight" );
        setDefaultCloseOperation( EXIT_ON_CLOSE );
     
        // gender group
        genderM = new JRadioButton("Male", true );
        genderF = new JRadioButton("Female", false );
        genderGroup = new ButtonGroup();
        genderGroup.add( genderM );
        genderGroup.add( genderF );
        genderPanel = new JPanel();
        genderPanel.setLayout( new BoxLayout( genderPanel, BoxLayout.Y_AXIS ) );
        genderPanel.add( new JLabel("Your Gender") );
        genderPanel.add( genderM );  genderPanel.add( genderF );
     
        // height group
        heightA = new JRadioButton("60 to 64 inches", true );
        heightB = new JRadioButton("64 to 68 inches", false );
        heightC = new JRadioButton("68 to 72 inches", false );
        heightD = new JRadioButton("72 to 76 inches", false );
        heightE = new JRadioButton("76 to 80 inches", false );
     
        heightGroup = new ButtonGroup();
        heightGroup.add( heightA ); heightGroup.add( heightB );
        heightGroup.add( heightC ); heightGroup.add( heightD );
        heightGroup.add( heightE );
     
        heightPanel = new JPanel();
        heightPanel.setLayout( new BoxLayout( heightPanel, BoxLayout.Y_AXIS ) );
        heightPanel.add( new JLabel("Your Height") );
        heightPanel.add( heightA ); heightPanel.add( heightB );
        heightPanel.add( heightC ); heightPanel.add( heightD );
        heightPanel.add( heightE );
     
        // result panel
        resultText  = new JTextField(7);
        resultText.setEditable( false );
        resultLabl  = new JLabel("Ideal Weight");
        resultPanel = new JPanel();
        resultPanel.add( resultLabl );
        resultPanel.add( resultText );
     
        // frame
        setLayout( new BorderLayout() );
        add( genderPanel, BorderLayout.WEST );
        add( heightPanel, BorderLayout.EAST );
        add( resultPanel, BorderLayout.SOUTH  );
     
      }
     
      public static void main ( String[] args )
      {
        Assignment4_Cheshire weightApp  = new Assignment4_Cheshire() ;
        weightApp.setSize( 250, 225 );
        weightApp.setResizable( false );
        weightApp.setVisible( true );
      }
    }

    Any guidance will be appreciacted.

    thanks
    Samantha


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Help with radio buttons and results.

    There may be a better way but you could always use the boolean method isSelected() for the RadioButtons.

    Also, how is your formula supposed to work for a range?
    if (genderM.isSelected())
    {
    if (heightA.isSelected())
    {
    Double temp = Math.pow(60,2) /28;
    resultText.setText(temp.toString());
    }

  3. #3
    Junior Member Bewitched1's Avatar
    Join Date
    Jul 2010
    Location
    Simpsonville SC
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with radio buttons and results.

    Thank you for the reply.

    The height range is used to calculate the weight range for either male or female.

    I have tried to get the radio buttons to work and I am still stuck. not sure where to add the calculations in.
    import java.awt.* ;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Assignment4_Cheshire extends JFrame
    {
      JRadioButton genderM, genderF;
      ButtonGroup  genderGroup;
      JPanel       genderPanel;
     
      JRadioButton heightA, heightB, heightC, heightD, heightE;
      ButtonGroup  heightGroup;
      JPanel       heightPanel;
     
      JTextField   resultText;
      JLabel       resultLabl;
      JPanel       resultPanel;
     
      public Assignment4_Cheshire()
      {
        setTitle( "Your Ideal Weight" );
        setDefaultCloseOperation( EXIT_ON_CLOSE );
     
        // gender group
        genderM = new JRadioButton("Male", true );
        genderF = new JRadioButton("Female", false );
        genderGroup = new ButtonGroup();
        genderGroup.add( genderM );
        genderGroup.add( genderF );
        genderPanel = new JPanel();
        genderPanel.setLayout( new BoxLayout( genderPanel, BoxLayout.Y_AXIS ) );
        genderPanel.add( new JLabel("Your Gender") );
        genderPanel.add( genderM );  genderPanel.add( genderF );
     
        // height group
        heightA = new JRadioButton("60 to 64 inches", true );
        heightB = new JRadioButton("64 to 68 inches", false );
        heightC = new JRadioButton("68 to 72 inches", false );
        heightD = new JRadioButton("72 to 76 inches", false );
        heightE = new JRadioButton("76 to 80 inches", false );
     
        heightGroup = new ButtonGroup();
        heightGroup.add( heightA ); heightGroup.add( heightB );
        heightGroup.add( heightC ); heightGroup.add( heightD );
        heightGroup.add( heightE );
     
        heightPanel = new JPanel();
        heightPanel.setLayout( new BoxLayout( heightPanel, BoxLayout.Y_AXIS ) );
        heightPanel.add( new JLabel("Your Height") );
        heightPanel.add( heightA ); heightPanel.add( heightB );
        heightPanel.add( heightC ); heightPanel.add( heightD );
        heightPanel.add( heightE );
     
        // result panel
        resultText  = new JTextField(7);
        resultText.setEditable( false );
        resultLabl  = new JLabel("Ideal Weight");
        resultPanel = new JPanel();
        resultPanel.add( resultLabl );
        resultPanel.add( resultText );
     
        // frame
        setLayout( new BorderLayout() );
        add( genderPanel, BorderLayout.WEST );
        add( heightPanel, BorderLayout.EAST );
        add( resultPanel, BorderLayout.SOUTH  );
     
      }
     
      public static void main ( String[] args )
      {
        Assignment4_Cheshire weightApp  = new Assignment4_Cheshire() ;
        weightApp.setSize( 250, 225 );
        weightApp.setResizable( false );
        weightApp.setVisible( true );
      }
    }

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help with radio buttons and results.

    Hello Bewitched1,

    Sorry I have only just found this thread. Did you solve this issue in the end?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. need help Coding RADIO BUTTON for REMEMBER ME
    By timosoft in forum Java Theory & Questions
    Replies: 3
    Last Post: February 4th, 2011, 05:19 PM
  2. Adding Radio Button Values
    By bazazu in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 13th, 2010, 10:41 AM
  3. Action from Radio Button
    By halfwaygone in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 25th, 2010, 11:52 AM
  4. radio buttons stop working
    By tabutcher in forum AWT / Java Swing
    Replies: 2
    Last Post: March 5th, 2010, 09:28 AM
  5. [SOLVED] Using html Radio Buttons with Servlets
    By oss_2008 in forum Java Servlet
    Replies: 2
    Last Post: June 25th, 2009, 06:39 AM

Tags for this Thread