Click to See Complete Forum and Search --> : problem with JRadioButtons


GroundRat
March 12th, 2005, 05:57 PM
I have the following problem and I've been working on it for the past two days at 8 hours at a time, and I'm at a stand still, please someone point me in the right direction. I'm taking a java2 class and one of the problems is to:
-converting a temperature on one scale to a temperature on another scale.
-use a multiple choice option ( Fahrenheit, Celcius, or Kelvin ) for input scale.
-use a multiple choice option ( same as above ) for output scale.

I've got the format set up right I know, but I can't seem to figure out how to pull the information from JTextField and individually distinguish between Fahrenheit, Celcius, or Kelvin.

Questions I have...
1. How do I seperate my variables in RadioBHandler in order to do the different calculations ConvertBHandler

P.S. how do you insert code into the thread with out saving it as an attatchment?
Code:
//@author: Byron T. Stuart

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;


public class StuartAssignment3 extends JFrame
{
private JTextField degree1;
private JRadioButton F1, F2, C1, C2, K1, K2;
private JLabel converted, label, label2, label3, label4, label5;
private JButton convertB, exitB;
private ConvertBHandler cbHandler;
private ExitBHandler ebHandler;
private ButtonGroup firstDegree, secondDegree;
private double f, c, k, f2, c2, k2, temp, temp1, temp2;

private static final int Width = 400;
private static final int Height = 250;

public StuartAssignment3()
{
//set up JLabel
converted = new JLabel(temp);
add(converted);
label = new JLabel("TO",SwingConstants.CENTER );
add(label);
label2 = new JLabel ("");
add(label2);
label3 = new JLabel ("");
add(label3);
label4 = new JLabel ("");
add(label4);
label5 = new JLabel ("");
add(label5);

//set up JTextField
degree1 = new JTextField(10);
add(degree1);

//Create Radio Buttons
F1 = new JRadioButton( "Fahrenheit", true );
F2 = new JRadioButton( "Fahrenheit", true);
C1 = new JRadioButton( "Celsius", false);
C2 = new JRadioButton( "Celsius", false);
K1 = new JRadioButton( "Kelvin", false);
K2 = new JRadioButton( "Kelvin", false);
add( F1 );
add( C1 );
add( K1 );
add( F2 );
add( C2 );
add( K2 );

//create logical relationship between JRadioButtons
firstDegree = new ButtonGroup();
secondDegree = new ButtonGroup();
firstDegree.add( F1 );
firstDegree.add( C1 );
firstDegree.add( K1 );
secondDegree.add( F2 );
secondDegree.add( C2 );
secondDegree.add( K2 );

//Create string objects
f = Double.parseDouble(degree1.getText());
c = Double.parseDouble(degree1.getText());
k = Double.parseDouble(degree1.getText());
f2 = Double.parseDouble(degree1.getText());
c2 = Double.parseDouble(degree1.getText());
k2 = Double.parseDouble(degree1.getText());


//register events for JRadioButtons
F1.addItemListener(
new RadioBHandler( f ) );
C1.addItemListener(
new RadioBHandler( c ) );
K1.addItemListener(
new RadioBHandler( k ) );
F2.addItemListener(
new RadioBHandler2( f2 ) );
C2.addItemListener(
new RadioBHandler2( c2 ) );
K2.addItemListener(
new RadioBHandler2( k2 ) );

//Convert Button
convertB = new JButton("Convert");
cbHandler = new ConvertBHandler();
convertB.addActionListener(cbHandler);

//Exit Button
exitB = new JButton("Exit");
ebHandler = new ExitBHandler();
exitB.addActionListener(ebHandler);

//Title
setTitle("Degree Conversions");

//Container
Container pane = getContentPane();

//Layout
pane.setLayout(new GridLayout(5,3));

//Components in the Pane
pane.add(degree1);
pane.add(label);
pane.add(converted);
pane.add(F1);
pane.add(label2);
pane.add(F2);
pane.add(C1);
pane.add(label3);
pane.add(C2);
pane.add(K1);
pane.add(label4);
pane.add(K2);
pane.add(convertB);
pane.add(label5);
pane.add(exitB);



//Size of window and display
setSize(Width,Height);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

//RadioBHandler
private class RadioBHandler implements ItemListener
{
private double temp1;

public RadioBHandler( double x )
{
temp1 = x;
}

public void itemStateChanged( ItemEvent event )
{
if( event.getStateChange() == ItemEvent.SELECTED )
temp = temp1;


}
}

//RadioBHandler2
private class RadioBHandler2 implements ItemListener
{
private double temp2;

public RadioBHandler2( double y)
{
temp2 = y;
}

public void itemStateChanged( ItemEvent event )
{


}
}

//Convert Button
private class ConvertBHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
temp = ItemEvent.SELECTED;



}
}

//Exit Button
private class ExitBHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}

public static void main(String[] args)
{
StuartAssignment3 degreeObject = new StuartAssignment3();
}
}