If statement?

I am trying to write a code that converts SAE to metric. I have to have Volume, Weight, and Lenght. under each one of those I need the option of gallon, quart, pint,cup under Volume. Under Weight Pound and ounce and under Length mile, yard, foot and inch.

currently I have a total of 4 classes. I have a Volume class, a Weight Class and a Length class that will accept the number inputed and do the conversion and send it to the main class.

I am trying to do it as an applet I start with the first 3 buttons when a choice is made those buttone are removed and the next set of buttons are redrawn. My problem is when the next choice is made it goes nowhere. I have the different options set up as methods and I am calling the methods.

This is the code I have so far for the main class. if I remove                          if(source == gallonButton)
       gallon();
it will compile but I don't know how to get to the next step. I know it should be simple I am just not getting it.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;

public class MetricProject12 extends JApplet implements ActionListener
{
      JButton volumeButton = new JButton("Volume");
      JButton lenghtButton = new JButton("Lenght");
      JButton weightButton = new JButton("Weight");
      
      JButton gallonButton = new JButton();
      JButton quartButton = new JButton();
      JButton pintButton = new JButton();
      JButton cupButton = new JButton();
      
      JButton poundButton = new JButton();
      JButton ounceButton = new JButton();
      
      JButton mileButton = new JButton();
      JButton yardButtton = new JButton();
      
      FlowLayout flow = new FlowLayout();
      Container con = getContentPane();
      
            JLabel mainLabel = new JLabel("Metric Conversion Project");
      JLabel optionExplainLabel = new JLabel("Please choose converstion type desired");


      
            public void init()
            {
            con.setLayout(flow);
            
            con.add(volumeButton);
            con.add(lenghtButton);
            con.add(weightButton);
            
            volumeButton.addActionListener(this);
            lenghtButton.addActionListener(this);
            weightButton.addActionListener(this);
            }//end init
            
            public void volume()
            {
                  remove (volumeButton);
                  remove (lenghtButton);
                  remove (weightButton);
                  repaint();
                  
                  FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
                  con.setLayout(flow);
                  
                  JButton gallonButton = new JButton();
                  JButton quartButton = new JButton();
                  JButton pintButton = new JButton();
                  JButton cupButton = new JButton();
                                    
                  con.add(gallonButton);
                  con.add(quartButton);
                  con.add(pintButton);
                  con.add(cupButton);
                  
                  gallonButton.addActionListener(this);
                  quartButton.addActionListener(this);
                  pintButton.addActionListener(this);
                  cupButton.addActionListener(this);
                  
                  gallonButton.setText("Gallon");
                  quartButton.setText("Quart");
                  pintButton.setText("Pint");
                  cupButton.setText("Cup");
                  
            }//end volume
            
            public void weight()
            {
                  remove (volumeButton);
                  remove (lenghtButton);
                  remove (weightButton);
                  repaint();
                  
                  FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
                  con.setLayout(flow);
                  
                  JButton poundButton = new JButton();
                  JButton ounceButton = new JButton();
                                    
                  con.add(poundButton);
                  con.add(ounceButton);
                  
                  poundButton.addActionListener(this);
                  ounceButton.addActionListener(this);
                  
                  poundButton.setText("Pound");
                  ounceButton.setText("Ounce");
                  


            }//end weight
            
            public void length()
            {
                  remove (volumeButton);
                  remove (lenghtButton);
                  remove (weightButton);
                  repaint();
                  
                  FlowLayout flow3 = new FlowLayout(FlowLayout.CENTER);
                  con.setLayout(flow3);

                  JButton mileButton = new JButton("Mile");
                  JButton yardButtton = new JButton("Yard");
                  JButton footButton = new JButton("Foot");
                  JButton inchButton = new JButton("inch");

                  con.add(mileButton);
                  con.add(yardButtton);
                  con.add(footButton);
                  con.add(inchButton);
            
                  mileButton.addActionListener(this);
                  yardButtton.addActionListener(this);
                  footButton.addActionListener(this);
                  inchButton.addActionListener(this);
                  
                  mileButton.setText("Mile");
                  yardButtton.setText("Yard");
                  footButton.setText("Foot");
                  inchButton.setText("Inch");
                  

            }//end lenght()
            
            public void gallon()
            {
                  remove (volumeButton);
                  remove (lenghtButton);
                  remove (weightButton);
                  repaint();
                  

                  String sGallon = JOptionPane.showInputDialog(null,"Please enter the number to be converted:");
                  double gallon = Double.parseDouble(sGallon);
                  
                  Volume vol = new Volume();
                  //creat an object from the Volume class
                  
                  vol.getGallon(gallon);
                  //pass the variable gallon to the getGallon method in the Volume class
                  
                  double liters;
                  liters = vol.getConvertGallon();

                  JLabel outputLabel = new JLabel();
                  outputLabel.setText(sGallon + "Gallons converted into liters is " + liters);
                  
            }//end gallon()
            
      public void actionPerformed(ActionEvent e)
      {
            Object source = e.getSource();
            {
                        if(source== volumeButton)
                        
                        volume();
                         if(source == gallonButton)
                         gallon();
                        
                        else if(source == weightButton)
                        
                              weight();
                        
                        else if(source == lenghtButton)
                        
                              length();
                        
            }//end object source
      }//end action performed
}//end class
tamihallAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
call validate() after changing the layout.
0
tamihallAuthor Commented:
I'm sorry, I am still pretty new at this. what do you mean?
0
objectsCommented:
eg.

          public void length()
          {
               con.remove (volumeButton);
               con.remove (lenghtButton);
               con.remove (weightButton);
               
               FlowLayout flow3 = new FlowLayout(FlowLayout.CENTER);
               con.setLayout(flow3);

               JButton mileButton = new JButton("Mile");
               JButton yardButtton = new JButton("Yard");
               JButton footButton = new JButton("Foot");
               JButton inchButton = new JButton("inch");

               con.add(mileButton);
               con.add(yardButtton);
               con.add(footButton);
               con.add(inchButton);
         
               mileButton.addActionListener(this);
               yardButtton.addActionListener(this);
               footButton.addActionListener(this);
               inchButton.addActionListener(this);
               
               mileButton.setText("Mile");
               yardButtton.setText("Yard");
               footButton.setText("Foot");
               inchButton.setText("Inch");
               
               con.validate();
               repaint();
          }//end lenght()
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

tamihallAuthor Commented:
I tried wat you said and tried the gallon() method linked to the mile button

else if(source == lenghtButton)
length();
if(source == mileButton)
 gallon();

complied it and it still did not work.
0
objectsCommented:
did you fix the gallon() method?
0
tamihallAuthor Commented:
theres nothing to fixing the gallon method. It is just being called. It is not reling on anything else in the main class I just changed how it was called.
0
objectsCommented:
the gallon() method has the same problem, you need to call validate().

you also need to remoive buttons from the content pane and not from the applet.
0
tamihallAuthor Commented:
I put
con.validate();
repaint();

at the end of the gallon() method and added
remove(mileButton);
remove(yardButtton);

to the method. I tried to remove the other two buttons and received an error. Complied it and still nothing.
0
objectsCommented:
you shouldn't be removing them from applet.
you add them to the content pane and thats where you should be removing them.
0
tamihallAuthor Commented:
Thank you for being so patent with me. I am not sure what you are trying to tell me to do. If you look in the other methods like volume() and weight(). I tell it to remove the buttons and repaint() and it worked there. So why wouldn't it work with the Gallon() method? And how would that keep the gallon() method from running?
0
objectsCommented:
are any exceptions being thrown?
0
objectsCommented:
what exactly happens when the gallon button is pressed?
0
tamihallAuthor Commented:
the way it stands right now, no. It just doesn't work. It compiles fine, but when I click on the mile button nothing happens.
0
tamihallAuthor Commented:
nothing.
0
objectsCommented:
>              JButton gallonButton = new JButton();

you create a local gallonButton
should be:

             gallonButton = new JButton();

though you don't need to recreate the button everytime.
0
tamihallAuthor Commented:
Maybe I am not explaining myself well enought. probably because I am so tired and frustrated.

When the applets starts I get a frame that has 3 buttons on it.

Volume     Weight      Lenght

If I press Volume I get a redrawn frame with 4 buttons

Gallon    Quart    pint    Cup

If I had pressed Weight I would get a redrwn screen with 2 buttons

Pound    Ounce

And if I had pressed Length I would get 4 buttons

mile    yard    foot   inch

This is what I get right now as it stands.
 I only have the gallon method written right now so that s the one I am useing to test.

After I press the Volume button and I get the next screen and I press the Gallon button it just sits there. nothing happens. I see all the buttons but I am not getting any reaction from them.

Object source = e.getSource();
   {
        if(source== volumeButton)
        volume();
            if(source == gallonButton)
       gallon();

        else if(source == weightButton)
                        
            weight();
                        
         else if(source == lenghtButton)
      length();

I want to be able to run if statements under the next set of buttons. Kind on the way I have it here. IE
if(source==gallonButton)
   gallon()
if(source == quartButton)
  quart()
if(source == pintButton)
  pint()
if(souce==cupButton)
  cup()

but it does not see any of the method and does not respond at all.

If I place the gallon() method at the same level as the volume() method it does see it and run
IE
Object source = e.getSource();
{
if(source== volumeButton)
volume();
gallon():

it will run like this so I know it is not the method.

I am going to be going to bed because I have 2 babys to chase tomorrow. I will check any responces tomorrow.

Thank you for your time and effort.


0
objectsCommented:
your gallon method will never get called because the source will never be gallonButton.
The gallonButton you add an action listener to is a local variable, and *not* the member variable gallonButton that you are comparing it to.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
esorfCommented:
So, in each of the volume(), length(), and weight() methods, just remove the lines that look like this:
               JButton mileButton = new JButton("Mile");
               JButton yardButtton = new JButton("Yard");
               JButton footButton = new JButton("Foot");
               JButton inchButton = new JButton("inch");
Since you've used the same names for local and class variables, things should compile.

I recommend you make and call a method like
    void clearWindow()
    {
               remove (volumeButton);
               remove (lenghtButton);
               remove (weightButton);
               repaint();
    }
Rather than repeating those lines all over.  You're going to find when things start working that there are more components to clear...
0
tamihallAuthor Commented:
objects

Thank you so much for all the help. I don't know why I didn't see that myself. I already had the buttons on top but I also had them in the method and that was why I was having so much trouble. Thank you again.
0
objectsCommented:
No worries :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.