Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: converter program, problem with action listener

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question converter program, problem with action listener

    here is my operater class and i am having trouble at line 92 and 215... it gives me the error:
    <anonymous operator$1> is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener


       import java.awt.*;
       import javax.swing.*;
       import java.awt.event.*;
       import java.util.*;
       import java.io.*;
       import javax.swing.event.*;
     
        public class operator
       {
          private Container c;
          ConvertArray conArr;
          JComboBox length1, length2, volume1, volume2, mass1, mass2, tempurate1, tempurate2, currency1, currency2;
     
          String first, second;
          int num = 0;
     
          JButton Ok = new JButton ("Convert");
          JButton cancel = new JButton ("cancel");
     
          JFrame ConLength = new JFrame ("converting length");
          JFrame ConVolume = new JFrame ("Converting Volume");
     
     
     
           public operator (ConvertArray i)
          {
             conArr = i;
          }
     
           public void Length () 
          {
     
     
             ConLength.setSize (500,400);
             ConLength.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
             ConLength.setVisible (true);
     
             JLabel addinfo = new JLabel ("Please select the first box for the value you are going to enter");
             JLabel addinfo2 = new JLabel ("select the second value for the value you want to convert to");
             JLabel addinfo3 = new JLabel ("Then click ok");	
             JLabel addinfo4 = new JLabel ("enter here:");
     
             length1 = new JComboBox ();
             length1.addItem ("cm");
             length1.addItem ("inch");
             length1.addItem ("feet");
             length1.addItem ("meters");
             length1.setBounds (70, 130, 150, 20);
     
     
             length2 = new JComboBox ();
             length2.addItem ("cm");
             length2.addItem ("inch");
             length2.addItem ("feet");
             length2.addItem ("meters");  
             length2.setBounds (250, 130, 150, 20);
     
     
             final JTextArea enter = new JTextArea (20, 130);
             enter.setBounds (250, 200, 150, 20); 
     
     
             addinfo.setBounds (10, 5, 400, 20);
             addinfo2.setBounds (10, 25, 400, 20);
             addinfo3.setBounds (10, 45, 400, 20);
             addinfo4.setBounds (70,200,150,20);
     
     
     
             Ok.setBounds (100, 275, 100, 50);
             cancel.setBounds (250, 275, 100, 50);
     
     
             c = ConLength.getContentPane ();
     
             c.setLayout (null);
     
             c.add(addinfo);
             c.add(addinfo2);
             c.add(addinfo3);  
             c.add (addinfo4);
             c.add(length1);
             c.add(length2);
             c.add(enter);
             c.add(Ok);
             c.add(cancel);
     
             ConLength.setVisible (true);    
     
     
             Ok.addActionListener (
                    new ActionListener()
                   {
     
                       public void itemStateChange (ItemEvent event, ItemEvent event2)
                      {
     
                         first = event.getItem ().toString ();
                         second = event2.getItem ().toString ();
     
     
                         ConLength.dispose ();
     
                      }
     
     
                       public void ActionPerformed (ActionEvent event)
                      {
                         String a = enter.getText ();
                         num = Integer.parseInt (a);
     
     
                         if ( first == "length")
                         {
     
     
     
                         }
     
     
     
     
                      }
                   }
     
                );
     
     
     
             cancel1 (); 
          }
     
           private void cancel1 ()
          {
             cancel.addActionListener (
                    new ActionListener ()
                   {
                       public void actionPerformed (ActionEvent event)
                      {
                         ConLength.dispose ();
                      }
                   }
     
     
                );
          }
     
     
     
       	// to convert voulme
     
     
           public void Voulme () 
          {
     
     
             ConVolume.setSize (500,400);
             ConVolume.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
             ConVolume.setVisible (true);
     
             JLabel addinfo = new JLabel ("Please select the first box for the value you are going to enter");
             JLabel addinfo2 = new JLabel ("select the second value for the value you want to convert to");
             JLabel addinfo3 = new JLabel ("Then click ok");	
             JLabel addinfo4 = new JLabel ("enter here:");
     
             volume1 = new JComboBox ();
             volume1.addItem ("Litres");
             volume1.addItem ("Gallons");
             volume1.addItem ("Oz.");
             volume1.addItem ("Millilitres");
             volume1.setBounds (70, 130, 150, 20);
     
     
             volume2 = new JComboBox ();
             volume2.addItem ("Litres");
             volume2.addItem ("Gallons");
             volume2.addItem ("Oz.");
             volume2.addItem ("Millilitres");  
             volume2.setBounds (250, 130, 150, 20);
     
     
             final JTextArea enter = new JTextArea (20, 130);
             enter.setBounds (250, 200, 150, 20); 
     
     
             addinfo.setBounds (10, 5, 400, 20);
             addinfo2.setBounds (10, 25, 400, 20);
             addinfo3.setBounds (10, 45, 400, 20);
             addinfo4.setBounds (70,200,150,20);
     
     
     
             Ok.setBounds (100, 275, 100, 50);
             cancel.setBounds (250, 275, 100, 50);
     
     
             c = ConVolume.getContentPane ();
     
             c.setLayout (null);
     
             c.add(addinfo);
             c.add(addinfo2);
             c.add(addinfo3);  
             c.add (addinfo4);
             c.add(length1);
             c.add(length2);
             c.add(enter);
             c.add(Ok);
             c.add(cancel);
     
             ConVolume.setVisible (true);    
     
     
             Ok.addActionListener (
                    new ActionListener()
                   {
     
                       public void itemStateChange (ItemEvent event, ItemEvent event2)
                      {
     
                         first = event.getItem ().toString ();
                         second = event2.getItem ().toString ();
     
                      }
     
     
                       public void ActionPerformed (ActionEvent event)
                      {
                         String a = enter.getText ();
                         num = Integer.parseInt (a);
     
     
                         if ( first == "Litres")
                         {
     
     
     
     
                         }
     
     
                         ConVolume.dispose ();
     
                      }
                   }
     
                );
     
             cancel2 (); 
          }
     
           private void cancel2 ()
          {
             cancel.addActionListener (
                    new ActionListener ()
                   {
                       public void actionPerformed (ActionEvent event)
                      {
                         ConVolume.dispose ();
                      }
                   }
     
                );
          }
       }




    Here is the running class:


     
       import java.io.*;
       import java.awt.*;
       import javax.swing.*;
       import java.awt.event.*;
       import java.util.*;
       import javax.swing.event.*;
     
     
        public class ConverterMenu extends JFrame implements ActionListener
       {
     
          private JMenu converterMenu, helpMenu, exitMenu;
          private JMenuItem Volume, length, mass, temperature, currency, Help, About, Exit;
     
          private JButton convert1,convert2,convert3;
          private Container contentArea;
     
          ConvertArray Ia = new ConvertArray ();
     
     
           ConverterMenu()  throws IOException
          {
     
     
     
             super ("Convert 9.0");
             setSize (400,500);
             setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
             setVisible (true);
     
             contentArea = getContentPane ();  
             contentArea.setLayout (null);
     
             ImageIcon scale = new ImageIcon ("scale2.gif");
             JLabel labelpic = new JLabel (scale);
             labelpic.setBounds (90, 10, 220, 170);
             contentArea.add (labelpic);
     
     
             convert1 = new JButton ("Convert Length");
             convert2 = new JButton ("Convert Volume");
             convert3 = new JButton ("Convert Currency");
     
             convert1.setBounds (100,200,200,50);
             convert2.setBounds (100,270,200,50);
             convert3.setBounds (100,340,200,50);
     
             convert1.addActionListener (this);
             convert2.addActionListener (this);
             convert3.addActionListener (this);
     
             contentArea.add(convert1);     
             contentArea.add(convert2);         
             contentArea.add(convert3);
     
     
             setContentPane (contentArea);
     
          	//adding items to the arrays from the files
     
             Ia.addItems (); 
     
          	// creating the drop down menu
     
     
             JMenuBar menuBar = new JMenuBar ();
             setJMenuBar (menuBar);
     
     
     
             converterMenu = new JMenu ("Convert");
             converterMenu.setMnemonic ('C');
             menuBar.add (converterMenu);
     
             helpMenu = new JMenu ("Help");
             helpMenu.setMnemonic ('H');
             menuBar.add (helpMenu);
     
             exitMenu = new JMenu ("Exit");
             exitMenu.setMnemonic ('X');
             menuBar.add (exitMenu);
     
             setMenuItems ();
     
             setVisible (true);
             setResizable (false);
     
          }
     
           public void actionPerformed (ActionEvent e)
          {
     
             if (e.getActionCommand ().equals ("Convert Length"))
             {
                try
                { 
     
                   operator op = new operator (Ia);
                   op.Length ();
     
                }
                    catch (Exception exp)
                   {
                   }
     
             }
     
          }
     
     
           private void setMenuItems ()
          {
     
             Volume = new JMenuItem ("Volume");
             Volume.setMnemonic ('o');
             //Volume.addActionListener (this);
             converterMenu.add(Volume);
     
             length = new JMenuItem ("Length");
             converterMenu.addSeparator ();
             length.setMnemonic ('L');
             length.addActionListener (this);
             converterMenu.add(length);
     
             mass = new JMenuItem ("Mass");
             converterMenu.addSeparator ();
             mass.setMnemonic ('M');
             //mass.addActionListener (this);
             converterMenu.add(mass);
     
             temperature = new JMenuItem ("Temperature");
             converterMenu.addSeparator ();
             temperature.setMnemonic ('T');
             //temperature.addActionListener (this);
             converterMenu.add(temperature);
     
             currency = new JMenuItem ("Currency");
             converterMenu.addSeparator ();
             currency.setMnemonic ('C');
          	//currency.addActionListener (this);
             converterMenu.add(currency);
     
             Help = new JMenuItem ("Help Menu");
          	//Help.addActionListener (this);
             Help.setMnemonic ('H');
             helpMenu.add (Help);
     
             About = new JMenuItem ("About prog.");
             About.setMnemonic ('A');
             helpMenu.add (About);
     
             Exit = new JMenuItem ("Exit");
             Exit.setMnemonic ('X');
             exitMenu.add (Exit);
     
          }
     
     
           public static void main (String [] args) throws IOException
          {
             new ConverterMenu ();
          }
     
       }

    im having a problem the Ok.addActionListener

    please help
    Last edited by robertson.basil; October 2nd, 2010 at 04:44 AM.


  2. #2
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: converter program, problem with action listener

    please help
    when you instantiate the new ActionListener you must overwrite the method actionPerformed(), so here is a small example you could use for your code:


        JButton b = new JButton("Push me"); // Erzeuge Button
        b.addActionListener( new ActionListener() { // anonymous inner class
            public void actionPerformed(ActionEvent e){ // Reaktion auf Event
                System.out.println("Au!");
            }
            });

    there are other ways to implement an ActionListener, but if you want to use an anonymous inner class the example above is ok.

  3. The Following User Says Thank You to j2me64 For This Useful Post:

    robertson.basil (October 2nd, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: converter program, problem with action listener

    Thanks for the help

Similar Threads

  1. ACTION LISTENER HANDLING
    By fari in forum What's Wrong With My Code?
    Replies: 19
    Last Post: June 12th, 2010, 08:31 PM
  2. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM
  3. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  4. Problem with Action Listener
    By JonoScho in forum AWT / Java Swing
    Replies: 4
    Last Post: March 19th, 2010, 01:03 AM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM