I am getting an error that says "The type Chapter165 must implement the inherited abstract method ActionListener.actionPerformed(Action Event). Can someone help me with where I went wrong?

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


class Chapter165 extends JFrame implements ActionListener
{
    
    
    
    private JTextField inputMile = new JTextField(10);
    private JTextField outputKm = new JTextField(10);
    
    private Chapter165(JTextField inputField, JTextField outputField) 
    {
    	          this.inputMile = inputMile; this.outputKm = outputField;
    	       }
    
    

    
    public Chapter165() 
    {
        
    	Chapter165 listener = new Chapter165(inputMile, outputKm);	
        inputMile.addActionListener(listener);
        outputKm.addActionListener(listener);

        //... Create content panel, set layout, add components
        JPanel content = new JPanel();
        content.setLayout(new GridLayout(2, 2));   
        content.add(new JLabel("Mile"));
        content.add(inputMile);                
        content.add(new JLabel("Kilometer"));
        content.add(outputKm);
        
        
        //... Set window characteristics
        setContentPane(content);
        pack();
        setLocationRelativeTo(null);
        setTitle("Exercise16_5");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        
        
    }
    
    

    
    
    
    
    
    
    
    
    class ConvertListener implements ActionListener 
    {
    	
    	 public void actionPerformed(ActionEvent e) 
    	 {
    		          if(e.getSource() == inputMile) 
    		          {
    		        	  double input = Double.parseDouble(inputMile.getText());  
   		        	   	  double milesKm = input * 1.6903;
    		              inputMile.setText("" + (input*1.6093));;
    		           } 
    		          else if(e.getSource() == outputKm) 
    		           {
    		        	   double input = Double.parseDouble(outputKm.getText());  
    		        	   double milesKm = input * 1.6903;
    		               outputKm.setText("" + (input*0.6213882));
    		           }
    		      }
    	
    	
    	
    	   
    	                  
    	        
    	    }
    	
    public static void main(String[] args) 
    {
        JFrame window = new Chapter165();
        
        
    
    }

	
	}

Recommended Answers

All 7 Replies

If you choose to implement the ActionListener interface you should also override the actionPerformed(ActionEvent); method. (java doc here)

class Chapter165 extends JFrame implements ActionListener {
    //bla bla bla
    @Override
    public void actionPerformed(ActionEvent e) {
	// TODO Auto-generated method stub
    }
}

(I guess you should also include add a button to your frame and associate an action to that button).

Here is a concept tutorial about interfaces:
http://java.sun.com/docs/books/tutorial/java/concepts/interface.html

Here is another tutorial about programming events (eg. ActionListener) in Java:
http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html

(PS: Don't skip the basics in Swing programmin & Read the Sun tutorials, they are wonderful).

I want the program to calculate when the enter button is pressed while in the text field. The override does not do anything while it is in the program. It takes away the error, but does not solve the problem.

This is the code with your override suggestion and what I could get from the tutorial. None of it is helping.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


class Chapter165 extends JFrame implements ActionListener
{
    
    
    private JTextField inputMile = new JTextField(10);
    private JTextField outputKm = new JTextField(10);
    
    private Chapter165(JTextField inputField, JTextField outputField) 
    {
    	          this.inputMile = inputMile; this.outputKm = outputKm;
    	       }
    
    

    
    public Chapter165() 
    {
        
    	Chapter165 listener = new Chapter165(inputMile, outputKm);	
        inputMile.addActionListener(listener);
        outputKm.addActionListener(listener);

        //... Create content panel, set layout, add components
        JPanel content = new JPanel();
        content.setLayout(new GridLayout(2, 2));   
        content.add(new JLabel("Mile"));
        content.add(inputMile);                
        content.add(new JLabel("Kilometer"));
        content.add(outputKm);
        
        
        //... Set window characteristics
        setContentPane(content);
        pack();
        setLocationRelativeTo(null);
        setTitle("Exercise16_5");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        
        
    }
    
    

    
    
    
    
    
    
    
    
    class ConvertListener implements ActionListener 
    {
    	
    	
    	
    	
    	
    	
    	 public void actionPerformed(ActionEvent e) 
    	 {
    		 
    		 

    		 
    		          if(e.getSource() == inputMile) 
    		          {
    		        	  
    		        	  String text = inputMile.getText();
    		    	        inputMile.selectAll();
    		        	  double input = Double.parseDouble(inputMile.getText());  
   		        	   	  double milesKm = input * 1.6903;
    		              inputMile.setText("" + (input*1.6093));;
    		           } 
    		          else if(e.getSource() == outputKm) 
    		           {
    		        	  String text = outputKm.getText();
    		    	        outputKm.selectAll();
    		        	   double input = Double.parseDouble(outputKm.getText());  
    		        	   double milesKm = input * 1.6903;
    		               outputKm.setText("" + (input*0.6213882));
    		           }
    		      }
    	
    	
    	
    	   
    	                  
    	        
    	    }
    	
    public static void main(String[] args) 
    {
        JFrame window = new Chapter165();
        
        
    
    }




	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}




			
	}

You created an inner class that implements ActionListener, and you defined the actionPerformed method for that inner class. But when you make the following statements:

Chapter165 listener = new Chapter165(inputMile, outputKm);
inputMile.addActionListener(listener);
outputKm.addActionListener(listener);

"listener" is an Object of the class type Chapter165. But in the Chapter165 class, you did not implement the actionPerformed method, and so, your code is not working as you wanted. If you get rid of that inner class, and put your actionPerformed method that you currently have in your inner class into your Chapter165 class, I think it will work.

P.S. if you wanted to do it like you have your code right now, you would need to change the code I posted above to this:

Chapter165 listener = new Chapter165(inputMile, outputKm);
inputMile.addActionListener(new ConvertListener());
outputKm.addActionListener(new ConvertListener());

If you don't understand why that is, feel free to ask questions. You can also read about inner classes and I think you should read about Listeners on sun's tutorials a little more.

import java.awt.event.*;

import javax.swing.*;

class Example2 extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JTextField ef;

    public Example2() {
	setSize(100, 100);
	ef = new JTextField();
	add(ef);

	ef.addActionListener(this);
	/* Press Enter after you enter text in the TextField */
	pack();
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
	JOptionPane.showMessageDialog(this, "Convert!");
    }

    public static void main(String args[]) {
	Example2 ex2 = new Example2();
	ex2.setVisible(true);
    }
}

I've a created a sample code for you.
Study and the code and "adapt" your own code in order to obtain the desired result.

Still, I strongly suggest you to study the basics in a more consistent fashion. It's for your own good...

commented: Thank you for your help. +1

Other things you should be aware of:


1. The two statements inside of your constructor do not do anything. You assigned inputMile equal to itself and then you assigned outputKm equal to itself. I think you meant to set inputMile to inputField and outputKm to outputField.

private Chapter165(JTextField inputField, JTextField outputField) 
    {
    	          this.inputMile = inputMile; 
                  this.outputKm = outputKm;
    }

2. If you used an IDE, such as Eclipse, a lot of your errors would be made apparent to you right off the bat. For example, the only reason I noticed the logical error in your code above is because I pasted your code into Eclipse and it immediately pointed out that your statement didn't do anything.

3. For example, other things that Eclipse pointed out to me about your code: you declared a variable named "text" inside of your if statement, but you never used that variable. Same thing goes for your variable "milesKm", which you declared in two different if statements but never used in either one.

4. You have statements in your code such as
"double input = Double.parseDouble(outputKm.getText());". If you read the javadoc for that method, you will see that if you pass in a String which cannot successfully be parsed as a double, (i.e. I passed it the String "///"), then it will throw an Exception. Currently, this Exception will crash your program, rendering it unusable. So you will need to read about "try catch" so that you can deal with that kind of situation so that your program can continue running and deal with the error smoothly.

5. You might want to think a little bit about your interface as well. Currently, once you make the changes I described in my first post of this thread, you will have a working product that actually does the conversions you wanted it to do and displays them in the fields. However, you might want to think about displaying the inputs in one field and the outputs in another -- i.e., if this was a calculator program your input field would not change from "3*5" to "15" -- you'd have two separate fields, one which would remain as "3*5", and the other which would say "15" once you hit enter. You might also want to have an "enter" button or something equivalent because right now, the only way to get the text fields to do what you want is to type a valid double into them and hit 'enter' on your keyboard.

Also, I realize I pointed out a lot of things that you didn't necessarily ask for help with, but don't take it harshly. Your program is a good first effort, I'm just offering advice on areas of possible improvement.

commented: Thank you for all of your help. You explained it very well. +1
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.