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

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




Cannot find OptionsListener

 
Reply to this topicStart new topic

Cannot find OptionsListener, CellPhone Class

haloegirl
21 Aug, 2007 - 04:23 PM
Post #1

New D.I.C Head
*

Joined: 1 Aug, 2007
Posts: 14


My Contributions
Hello, I am finished createing a cellPhone class and it has only two errors and I cannot see where I went wrong..Could someone please take a look with some fresh eyes I would really appriciate it.

My Error is: Cannot find OptionsListener (lines 179 and 180)

CODE

/**
    The CellPhone class allows the user to select
    a cell phone package, a cell phone, and any
    additional options.
*/

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


public class CellPhone extends JFrame
{
    //Sales Tax rate
    private final double Tax_Rate = 0.06;
    
    //Array to hold users choices
    private String [] choices = {"Model 100: $29.95 + tax", "300 minutes per month: $45.00 per month",
                                         "Voicemail NOT included", "Text messaging NOT included."};
    private JLabel phoneCostLabel;
    private JLabel monthlyCostLabel;
    private JTextField phoneCostOutput;
    private JTextField monthlyCostOutput;
    private JPanel listPanel;
    private JPanel pricePanel;
    private JPanel instructionsPanel;
    private JLabel instructions;
    private JList list;
    private Container contentPane;
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenuItem exitItem;
    private JMenu packageMenu;
    private JMenuItem minutes300;
    private JMenuItem minutes800;
    private JMenuItem minutes1500;
    private JMenu phoneMenu;
    private JMenuItem model100;
    private JMenuItem model110;
    private JMenuItem model200;
    private JMenu optionsMenu;
    private JMenuItem voiceItem;
    private JMenuItem textItem;
    private double phoneCost;
    private double packageCost;
    private double optionsCost;
        
    DecimalFormat money = new DecimalFormat("0.00");
    
    /*
         Constructor
     */     
    public CellPhone()
    {
        //Create a Title Window
        super("Cell Phone Packages");
        //Exit when close button is clicked
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Get content pane and set layout
        contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        //Menu Bar
        buildMenuBar();
        //Instructions panel
        buildInstructionsPanel();
        //Image Panel
        buildListPanel();
        //output panel
        buildPricePanel();
        //Add panels to window
        contentPane.add(instructionsPanel, BorderLayout.NORTH);
        contentPane.add(listPanel, BorderLayout.CENTER);
        contentPane.add(pricePanel, BorderLayout.SOUTH);
        //contentPanel.add(buttonPanel, BorderLayout.SOUTH);
        //Create User Choice Array
        pack();
        setVisible(true);    
    }
    /*
         buildMenuBar method creates a menu bar
     */
    public void buildMenuBar()
    {
        //to create a menu bar
        menuBar = new JMenuBar();
        //To build menu's
        buildPhoneMenu();
        buildPackageMenu();
        buildOptionsMenu();
        buildFileMenu();
            
        //To add menu's to menus bar
        menuBar.add(fileMenu);
        menuBar.add(packageMenu);
        menuBar.add(phoneMenu);
        menuBar.add(optionsMenu);
        //To place menu bar in window
        setJMenuBar(menuBar);
    }
    /*
         The buildInstructionsPanel method
         Contains instructions for the user
     */
     public void buildInstructionsPanel()
     {
         //Create a panel
         instructionsPanel = new JPanel();
         //Create label with user instructions
         instructions = new JLabel("Choose a phone, package and additional options.");
         //Add label to panel
         instructionsPanel.add(instructions);
     }
     /*
          The buildPhoneMenu method
          Creates a drop down menu
      */
      public void buildPhoneMenu()
      {
          //create items for the phone menu
          model100 = new JRadioButtonMenuItem("Model 100", true);
          phoneCost = (1 + Tax_Rate) * 29.95;
          model110 = new JRadioButtonMenuItem("Model 110");
          model200 = new JRadioButtonMenuItem("Model 200");
          //To connect each with a listener event
          model100.addActionListener(new PhoneListener());
          model110.addActionListener(new PhoneListener());
          model200.addActionListener(new PhoneListener());
          //Radio Buttons
          ButtonGroup group = new ButtonGroup();
          group.add(model100);
          group.add(model110);
          group.add(model200);
          //Create Menu
          phoneMenu = new JMenu("Phones");
          phoneMenu.add(model100);
          phoneMenu.add(model110);
          phoneMenu.add(model200);
      }
      /*
           The buildPackageMenu method
           gives the user a drop down menu with package options           
       */
       public void buildPackageMenu()
       {
               //Create items
               minutes300 = new JRadioButtonMenuItem("300 minutes monthly", true);
               packageCost = 45.0;
               minutes800 = new JRadioButtonMenuItem("800 minutes monthly");
               minutes1500 = new JRadioButtonMenuItem("1500 minutes monthly");
               
               //Add each to listener
               minutes300.addActionListener(new PackageListener());
               minutes800.addActionListener(new PackageListener());
               minutes1500.addActionListener(new PackageListener());
               //Radio Buttons
               ButtonGroup group = new ButtonGroup();  
               group.add(minutes300);
               group.add(minutes800);
               group.add(minutes1500);
               //Create a menu
               packageMenu = new JMenu("Packages");
               packageMenu.add(minutes300);
               packageMenu.add(minutes800);
               packageMenu.add(minutes1500);
       }
       /*
            buildOptionsMenu method
            gives user choices of cell phoneoptions
        */
        public void buildOptionsMenu()
        {
            //Create Items
            voiceItem = new JCheckBoxMenuItem("Voice mail");
            textItem = new JCheckBoxMenuItem("Text Messaging");
            optionsCost = 0;
            //add each to Listener
            voiceItem.addActionListener(new OptionsListener());
            textItem.addActionListener(new OptionsListener());
            //create menu
            optionsMenu = new JMenu("Options");
            //Add items to the menu
            optionsMenu.add(voiceItem);
            optionsMenu.add(textItem);
        }
        /*
             buildFileMenu method
             for file commands
         */
         public void buildFileMenu()
         {
             //create item
             exitItem = new JMenuItem("Exit");
             //short cut key
             exitItem.setMnemonic(KeyEvent.VK_X);
             //add to a listener
             exitItem.addActionListener(new ExitListener());
             //Create menu
             fileMenu = new JMenu("File");
             //create a shortcut key
             fileMenu.setMnemonic(KeyEvent.VK_F);
             //add item to menu
             fileMenu.add(exitItem);
         }
         /*
              buildListPanel method
              gives the user a list of choices
          */
          public void buildListPanel()
          {
              //create panel
              listPanel = new JPanel();
              //Create label
              list = new JList(choices);
              //Set Number of visible Rows
              list.setVisibleRowCount(6);
              //add to panel
              listPanel.add(list);
          }
          /*
               The buildPricePanel method
               creates a panel to display cost
           */
           public void buildPricePanel()
           {
                   //Create panel
                   pricePanel = new JPanel();
                   pricePanel.setLayout(new GridLayout(2, 2));
                   //Create Label for phone cost
                   phoneCostLabel = new JLabel("Phone Cost");
                   phoneCostOutput = new JTextField(10);
                   phoneCostOutput.setEditable(false);
                   phoneCostOutput.setText("$" + money.format(phoneCost));
                   //Create label for monthly cost
                   monthlyCostLabel = new JLabel("Monthly Bill");
                   monthlyCostOutput = new JTextField(10);
                   monthlyCostOutput.setEditable(false);
                   monthlyCostOutput.setText("$" + money.format(packageCost + optionsCost));
                   //Add to Panel
                   pricePanel.add(phoneCostLabel);
                   pricePanel.add(phoneCostOutput);
                   pricePanel.add(monthlyCostLabel);
                   pricePanel.add(monthlyCostOutput);
           }
           /*
                Private inner class that handles an event
            */
            private class ExitListener implements ActionListener
            {
                public void actionPerformed(ActionEvent e)
                {
                    System.exit(0);
                }
            }
            /*
                 Private inner class that handles the event
             */
             private class PhoneListener implements ActionListener
             {
                 public void actionPerformed(ActionEvent e)
                 {
                     //Calculate price and set output
                     if(model100.isSelected())
                     {
                         phoneCost = (1 + Tax_Rate) * 29.95;
                         choices[0] = "Model 100: $29.95 + tax";                                       
                     }
                     else if(model110.isSelected())
                     {
                         phoneCost = (1 + Tax_Rate) * 49.95;
                         choices[0] = "Model 110: $49.95 + tax";
                     }
                     else if(model200.isSelected())
                     {
                         phoneCost = (1 + Tax_Rate) * 99.95;
                         choices[0] = "Model 200: 99.95 + tax";
                     }
                     //Display
                     list.setListData(choices);
                     phoneCostOutput.setText("$" + money.format(phoneCost));
                 }
             }
             /*
                  Private inner class that handles an event
              */
              private class PackageListener implements ActionListener
              {
                  public void actionPerformed(ActionEvent e)
                  {
                      //Change price depending on users choices
                      if(minutes300.isSelected())
                      {
                          choices[1] = "300 minutes per month: $45.00 per month";
                          packageCost = 45.0;
                      }
                      else if(minutes800.isSelected())
                      {
                          choices[1] = "800 minutes per month: $65.00 per month";
                          packageCost = 65.0;
                      }
                      else if(minutes1500.isSelected())
                      {
                          choices[1] = "1500 minutes per month: $99.00 per month";
                          packageCost = 99.0;
                      }
                      //Display
                      monthlyCostOutput.setText("$" + money.format(packageCost + optionsCost));
                      list.setListData(choices);
                  }
              }
              /*
                   Private inner class that handles events
               */
               private class actionPerformed implements ActionListener
               {
                   public void actionPerformed(ActionEvent e)
                   {
                       //reset options
                       optionsCost = 0;
                       //To change price depending on users choice
                       if(voiceItem.isSelected())
                       {
                           choices[2] = "Voice mail: $5.00 per month extra";
                           optionsCost = 5.0;
                       }
                       else
                       {
                           choices[2] = "Voice mail is NOT included";
                       }
                       if(textItem.isSelected())
                       {
                           choices[3] = "Text messanging: $10.00 per month extra";
                           optionsCost = 10.0;
                       }
                       else
                       {
                           choices[3] = "Text messaging NOT included";
                       }
                       //Display
                       monthlyCostOutput.setText("$" + money.format(packageCost + optionsCost));
                       list.setListData(choices);
                   }
               }
             }


This post has been edited by skyhawk133: 21 Aug, 2007 - 04:46 PM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Cannot Find OptionsListener
21 Aug, 2007 - 04:33 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
you define your listeners in classes, but you have not defined a subclass named: OptionsListener which extends actionlistener. Thus this class does not exist and the error occurs.

This is not a default class either.

add the listener and it will work.
User is offlineProfile CardPM
+Quote Post

haloegirl
RE: Cannot Find OptionsListener
21 Aug, 2007 - 04:43 PM
Post #3

New D.I.C Head
*

Joined: 1 Aug, 2007
Posts: 14


My Contributions
QUOTE(William_Wilson @ 21 Aug, 2007 - 05:33 PM) *

you define your listeners in classes, but you have not defined a subclass named: OptionsListener which extends actionlistener. Thus this class does not exist and the error occurs.

This is not a default class either.

add the listener and it will work.



Ahhh I cant believe that I missed that....thanks it works now that I made the changes..... cool.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:24AM

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