javaitemlistener

Myframe is not abstract and does not override abstract method


I am new to the Java programming. I am just doing a program to select the gender using checkbox. but when I run the program I am getting an error as

Myframe is not abstract and does not override abstract method itemStateChanged(ItemEvent) in ItemListener

The code is as follows

import java.awt.event.*;
class Myframe extends Frame implements ItemListener{
    CheckboxGroup cbg;
    Checkbox chkmale,chkfemale;
    Label lblres;

    void init(){
        cbg = new CheckboxGroup();
        chkmale = new Checkbox();
        chkfemale = new Checkbox();
        lblres = new Label();

        add(chkmale); add(chkfemale); add(lblres);

        this.addWindowListener(new MyListner());
        chkmale.addItemListener(this);
        chkfemale.addItemListener(this);
    }
    public void itemstateChanged(java.awt.event.ItemEvent ie){
        if(chkmale.getState() == true){
            lblres.setText("Male");
        }
        else{
            lblres.setText("Female");
        }
    }
}


Solution

  • This simply because you're incorrectly implementing the java.awt.event.ItemListener interface. It should be itemStateChanged instead of itemstateChanged:

    import java.awt.*;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    class Myframe extends Frame implements ItemListener {
        ...
    
        @Override
        public void itemStateChanged(ItemEvent e) {
            // Your code here.
        }
    }
    

    Don't forget to add @Override so that you won't forget that the method is implementing the interface.