Welcome to Dream.In.Code
Become a Java Expert!

Join 150,406 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 932 people online right now. Registration is fast and FREE... Join Now!




Using Radio buttons

 
Reply to this topicStart new topic

Using Radio buttons

jenny
16 Mar, 2008 - 01:27 AM
Post #1

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 4

this program is already running, but i don't know how to control
2 radio buttons at the same time.. coz the condition here is that you have to chose if male and female, and then chose your height, then the output will be displayed in the resultText. help me please.. thank you!
java

import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
public class IdealWeight extends JFrame implements ActionListener {
JRadioButton genderM, genderF;
ButtonGroup genderGroup;
JPanel genderPanel;
JRadioButton heightA, heightB, heightC, heightD, heightE;
ButtonGroup heightGroup;
JPanel heightPanel;
JTextField resultText;
JLabel resultLabl;
JPanel resultPanel;

String height1 = "60 to 64 inches";
String height2 = "64 to 68 inches";
String height3 = "68 to 72 inches";
String height4 = "72 to 76 inches";
String height5 = "76 to 80 inches";
// String height6 = "height6";
String fem = "female";
String mal = "male";
public IdealWeight() {
setTitle( "Your Ideal Weight" );
setDefaultCloseOperation( EXIT_ON_CLOSE );
// gender group
genderM = new JRadioButton(mal, true );
genderM.setActionCommand(mal);
genderM.addActionListener(this);
genderF = new JRadioButton(fem, false );
genderF.setActionCommand(fem);
genderF.addActionListener(this);

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(height1, true );
heightA .setActionCommand(height1 );
heightA .addActionListener(this);

heightB = new JRadioButton(height2 , false );
heightB .setActionCommand( height2 );
heightB .addActionListener(this);

heightC = new JRadioButton(height3, false );
heightC.setActionCommand(height3);
heightC.addActionListener(this);

heightD = new JRadioButton(height4, false );
heightD.setActionCommand(height4);
heightD.addActionListener(this);

heightE = new JRadioButton(height5 , false );
heightE .setActionCommand(height5 );
heightE .addActionListener(this);

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 );
// content pane
getContentPane().setLayout( new BorderLayout() );
getContentPane().add( genderPanel, BorderLayout.WEST );
getContentPane().add( heightPanel, BorderLayout.EAST );
getContentPane().add( resultPanel, BorderLayout.SOUTH ); }

public void actionPerformed(ActionEvent evt) {

String get = evt.getActionCommand();

if (get.equals ("female")) { //&& (get.equals("60 to 64 inches"))

if(heightA.isSelected("60 to 64 inches"))

//{
resultText.setText("Hello");
//}

}
if (get.equals ("male"))

resultText.setText("World");
}


public static void main ( String[] args ) {
IdealWeight weightApp = new IdealWeight() ;
weightApp.setSize( 250, 225 );
weightApp.setVisible( true ); } }


aimport java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
public class IdealWeight extends JFrame implements ActionListener {
JRadioButton genderM, genderF;
ButtonGroup genderGroup;
JPanel genderPanel;
JRadioButton heightA, heightB, heightC, heightD, heightE;
ButtonGroup heightGroup;
JPanel heightPanel;
JTextField resultText;
JLabel resultLabl;
JPanel resultPanel;

String height1 = "60 to 64 inches";
String height2 = "64 to 68 inches";
String height3 = "68 to 72 inches";
String height4 = "72 to 76 inches";
String height5 = "76 to 80 inches";
// String height6 = "height6";
String fem = "female";
String mal = "male";
public IdealWeight() {
setTitle( "Your Ideal Weight" );
setDefaultCloseOperation( EXIT_ON_CLOSE );
// gender group
genderM = new JRadioButton(mal, true );
genderM.setActionCommand(mal);
genderM.addActionListener(this);
genderF = new JRadioButton(fem, false );
genderF.setActionCommand(fem);
genderF.addActionListener(this);

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(height1, true );
heightA .setActionCommand(height1 );
heightA .addActionListener(this);

heightB = new JRadioButton(height2 , false );
heightB .setActionCommand( height2 );
heightB .addActionListener(this);

heightC = new JRadioButton(height3, false );
heightC.setActionCommand(height3);
heightC.addActionListener(this);

heightD = new JRadioButton(height4, false );
heightD.setActionCommand(height4);
heightD.addActionListener(this);

heightE = new JRadioButton(height5 , false );
heightE .setActionCommand(height5 );
heightE .addActionListener(this);

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 );
// content pane
getContentPane().setLayout( new BorderLayout() );
getContentPane().add( genderPanel, BorderLayout.WEST );
getContentPane().add( heightPanel, BorderLayout.EAST );
getContentPane().add( resultPanel, BorderLayout.SOUTH ); }

public void actionPerformed(ActionEvent evt) {

String get = evt.getActionCommand();

if (get.equals ("female")) { //&& (get.equals("60 to 64 inches"))

if(heightA.isSelected("60 to 64 inches"))

//{
resultText.setText("Hello"); // this is just for testing
//}

}
if (get.equals ("male"))

resultText.setText("World");// this is just for testing
}


public static void main ( String[] args ) {
IdealWeight weightApp = new IdealWeight() ;
weightApp.setSize( 250, 225 );
weightApp.setVisible( true ); } }

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Using Radio Buttons
16 Mar, 2008 - 04:56 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Next time please use code tag code.gif
Here is a brief tutorial on the radio buttons.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:24PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month