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!

Recommended Answers

All 3 Replies

It's hard to know where to start with so little info about the problem. You say you "cannot get the radio buttons to work". What exactly does that mean? They don't appear, they don't change when you click them, your listener isn't called, your listener is called but doesn't do what you expect? How can ou even know that if the code doesn't compile in the first place.
Smeone here will help you, but first you need to explain exactly and precisely what is going wrong, and post the complete un-edited text of any error messages you get.

Morning,
Sleep is a wonderful thing.
I woke up knowing how to fix most of the problems:)
First I needed to move all my textboxes to global objects, since I am using them in different methods.
This acctually fixed the buttons not working.
Now I cannot get the math to work properly :(
When you hit "Calculate" the volume comes back 0.
I am sure I will figure it out. I have a smiliar method in another program.
I want to post what I was able to figure out incase someone has a similar issue.

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;
import java.awt.Color;
import java.awt.FlowLayout;



public class TestPool extends JFrame
{
    JFrame mFrame;
    JTabbedPane jtp;
    JPanel jpVolumeCalc;
    ButtonGroup radial;
    JRadioButton jbrPool;
    JRadioButton jbrOvalTub;
    JRadioButton jbrRoundTub;
    RadioListener myListener;

    JLabel jlHeight;
    JTextField jtfHeight;
    JLabel jlLength;
    JTextField jtfLength;
    JLabel jlWidth;
    JTextField jtfWidth;
    JTextField jtfVolume;

    JButton jbCalculate;
    CalcBtnHandler calHandler;


public TestPool()
{
    mFrame = new JFrame();
    mFrame.setVisible(true);
    mFrame.setSize(600,600);

    jtp = new JTabbedPane();//container for tabs
    jtp.setVisible(true);
    jpVolumeCalc = new JPanel();//volume calculator tab

    ///items for the VolumeCalc tab
    radial = new ButtonGroup();
    //create radial buttons
    jbrPool = new JRadioButton("Pool", false);
    jbrPool.setActionCommand("pool");//to do-create action command for oval
    radial.add(jbrPool);
    jpVolumeCalc.add(jbrPool);
    jbrOvalTub = new JRadioButton("Oval Hot Tub",false);
    jbrOvalTub.setActionCommand("oval");//to do-create action command for oval
    radial.add(jbrOvalTub);
    jpVolumeCalc.add(jbrOvalTub);
    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
    myListener = new RadioListener();
    jbrPool.addActionListener(myListener);
    jbrOvalTub.addActionListener(myListener);
    jbrRoundTub.addActionListener(myListener);


    //depth set
    jlHeight = new JLabel();
    jlHeight.setText("Depth: ");
    jpVolumeCalc.add(jlHeight);
    jtfHeight = new JTextField(4);
    jtfHeight.setText("");
    jpVolumeCalc.add(jtfHeight);

    //length set
    jlLength = new JLabel();
    jlLength.setText("Length: ");
    jpVolumeCalc.add(jlLength);
    jtfLength = new JTextField(4);
    jtfLength.setText("");
    jpVolumeCalc.add(jtfLength);

    //width set
    jlWidth = new JLabel();
    jlWidth.setText("Width: ");
    jpVolumeCalc.add(jlWidth);
    jtfWidth = new JTextField(4);
    jtfWidth.setText("");
    jpVolumeCalc.add(jtfWidth);

    //volume set
    JLabel jlVolume = new JLabel();
    jlVolume.setText("Volume: ");
    jpVolumeCalc.add(jlVolume);
    jtfVolume = new JTextField(4);
    jtfVolume.setEditable(false);
    jpVolumeCalc.add(jtfVolume);

    jbCalculate = new JButton("Calculate");
    jbCalculate.setVisible(true);
    jpVolumeCalc.add(jbCalculate);
    jbCalculate.setMnemonic('C');
    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);
            jtfWidth.setEditable(true);
        }
        if("oval".equals(r.getActionCommand()))
        {
            jbrPool.setSelected(false);
            jbrOvalTub.setSelected(true);
            jbrRoundTub.setSelected(false);
            jtfWidth.setEditable(true);
        }
        if("round".equals(r.getActionCommand()))
        {
            jbrPool.setSelected(false);
            jbrOvalTub.setSelected(false);
            jbrRoundTub.setSelected(true);
            jtfWidth.setEditable(false);
        }
    }
}//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(""))
        {
            error();
            jtfHeight.setBackground(Color.YELLOW);
        }
        depth = Double.parseDouble(dep);

        len = jtfLength.getText();
        if (len.equals(""))
        {
            error();
            jtfLength.setBackground(Color.YELLOW);
        }
        length = Double.parseDouble(len);

        wid = jtfWidth.getText();
        if (wid.equals(""))
        {
            error();
            jtfWidth.setBackground(Color.YELLOW);
        }
        width = Double.parseDouble(wid);

        //the math
        volume = ((width * depth) * length);
        jtfVolume.setText(num.format(volume)); // i think this needs to be parsed to a string from a double...
    }//end pool

    jtfVolume.setText("0");

}
}//end of CalcBtnHandler

public static void error()
{
    JOptionPane.showMessageDialog(null, "Please fill in all appropriate fields.)", "Empty Field", JOptionPane.ERROR_MESSAGE);
}

public static void main(String[] args)
        {
            TestPool psd = new TestPool();
            psd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }//end main
}

I really am such a "blonde" today. On line 184 i have jtfVolume.setText("0"); that needs to be part of the error checking, not a statement.
Take out that line and it all works.

Sleep is AWESOME!!

I really hope this helps someone else.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.