Hello,
I am trying to write a program that will calculate the volume of a hole to be dug for a pool or spa. I have some radio buttons that the use can select. Once a button is chosem, the user enters information and then can hit the calculate button to get the volume of the hole.
I have already checked the GUI, and the math. I cannot get the radio buttons to work, nor can I get it to compile with the GUI and the calculate method in the same program. I get "cannot find symbol jbr.setSelected(true);" for all my radio buttons and textFields.
I have been staring and tinkering for hours and could use some help. I know it is something obvious. I just cannot grasp it.
Here is the code I am working on. Some of the imports are for the other parts of the program. I pulled this part out to try and figure out why it will not work.
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.lang.Math;
import java.text.DecimalFormat;
import javax.swing.JButton;
public class TestPool extends JFrame
{
public TestPool()
{
JFrame mFrame = new JFrame();
mFrame.setVisible(true);
mFrame.setSize(600,600);
JTabbedPane jtp = new JTabbedPane();//container for tabs
jtp.setVisible(true);
JPanel jpVolumeCalc = new JPanel();//volume calculator tab
///items for the VolumeCalc tab
ButtonGroup radial = new ButtonGroup();
//create radial buttons
JRadioButton jbrPool = new JRadioButton("Pool", false);
jbrPool.setActionCommand("pool");//to do-create action command for oval
radial.add(jbrPool);
jpVolumeCalc.add(jbrPool);
JRadioButton jbrOvalTub = new JRadioButton("Oval Hot Tub",false);
jbrOvalTub.setActionCommand("oval");//to do-create action command for oval
radial.add(jbrOvalTub);
jpVolumeCalc.add(jbrOvalTub);
JRadioButton jbrRoundTub = new JRadioButton("Round Hot Tub", false);
jbrRoundTub.setActionCommand("round");//to do-create action command for round
radial.add(jbrRoundTub);
jpVolumeCalc.add(jbrRoundTub);
//group radial buttons
//radiobutton listener
RadioListener myListener = new RadioListener();
jbrPool.addActionListener(myListener);
jbrOvalTub.addActionListener(myListener);
jbrRoundTub.addActionListener(myListener);
//depth set
JLabel jlHeight = new JLabel();
jlHeight.setText("Depth: ");
jpVolumeCalc.add(jlHeight);
JTextField jtfHeight = new JTextField(4);
jtfHeight.setText("");
jpVolumeCalc.add(jtfHeight);
//length set
JLabel jlLength = new JLabel();
jlLength.setText("Length: ");
jpVolumeCalc.add(jlLength);
JTextField jtfLength = new JTextField(4);
jtfLength.setText("");
jpVolumeCalc.add(jtfLength);
//width set
JLabel jlWidth = new JLabel();
jlWidth.setText("Width: ");
jpVolumeCalc.add(jlWidth);
JTextField jtfWidth = new JTextField(4);
jtfWidth.setText("");
jpVolumeCalc.add(jtfWidth);
//volume set
JLabel jlVolume = new JLabel();
jlVolume.setText("Volume: ");
jpVolumeCalc.add(jlVolume);
JTextField jtfVolume = new JTextField(4);
jtfVolume.setEditable(false);
jpVolumeCalc.add(jtfVolume);
JButton jbCalculate = new JButton("Calculate");
jbCalculate.setVisible(true);
jpVolumeCalc.add(jbCalculate);
jbCalculate.setMnemonic('C');
CalcBtnHandler calHandler = new CalcBtnHandler();
jbCalculate.addActionListener(calHandler);
jtp.addTab("Volume Calculator",jpVolumeCalc);
mFrame.add(jtp);
}//end TestPool constructor
class RadioListener implements ActionListener
{
public void actionPerformed(ActionEvent r)
{
if("pool".equals(r.getActionCommand()))
{
jbrPool.setSelected(true);
jbrOvalTub.setSelected(false);
jbrRoundTub.setSelected(false);
}
if("oval".equals(r.getActionCommand()))
{
jbrPool.setSelected(false);
jbrOvalTub.setSelected(true);
jbrRoundTub.setSelected(false);
}
if("round".equals(r.getActionCommand()))
{
jbrPool.setSelected(false);
jbrOvalTub.setSelected(false);
jbrRoundTub.setSelected(true);
}
}
}//end RadioListener
class CalcBtnHandler implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
DecimalFormat num = new DecimalFormat("#,###.##");
double width, length, depth, volume;
String wid, len, dep, vol;
double pi = Math.PI;
if(jbrPool.isSelected())
{
//pull information from text fields
dep = jtfHeight.getText();
if (dep.equals(""))
{
dep = ("0");
jtfHeight.setText("0");
}
depth = Double.parseDouble(dep);
len = jtfLength.getText();
if (len.equals(""))
{
len = ("0");
jtfLength.setText("0");
}
length = Double.parseDouble(len);
wid = jtfWidth.getText();
if (wid.equals(""))
{
wid = ("0");
jtfWidth.setText("0");
}
width = Double.parseDouble(wid);
//the math
vol = wid * dep * len;
jtfVolume.setText(num.format(vol));
}//end pool
jtfVolume.setText("0");
}
}//end of CalcBtnHandler
}
Thanks a bunch if you can point me in the right direction!