Results 1 to 2 of 2
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default abstract classes

    I am trying to create a simple action listener. I am getting an error that says:

    Java Code:
    GUI.buttonHandler is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
    my code:

    Java Code:
    public class GUI extends JFrame {
    
        private static final int WIDTH = 400;
        private static final int HEIGHT = 300;
        private JLabel name;
        private JTextField textField;
        private JButton submit;
        private buttonHandler btnHandler;
    
        public static void main(String[] args) {
            GUI gui = new GUI();
        }
    
        public GUI() {
            setTitle("A sameple");
            setSize(WIDTH, HEIGHT);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            name = new JLabel("Name: ", SwingConstants.LEFT);
            textField = new JTextField(10);
            submit = new JButton("Submit");
            btnHandler = new buttonHandler();
            submit.addActionListener(btnHandler);
            setTitle("Sample Title");
            Container pane = getContentPane();
            pane.setLayout(new GridLayout(4, 2));
            
            pane.add(name);
            pane.add(textField);
            pane.add(submit);
            
            setSize(WIDTH, HEIGHT);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        private class buttonHandler implements ActionListener // it does not like buttonHandler here
        {
            public void actionPeformed(ActionEvent e)
            {
                textField.setText("Good!!!!(:");
            }
        }
    If you could please explain what I am doing wrong, because I have done research, and I can't understand. It has to do with the subclass, or something of the sort?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: abstract classes

    does not override abstract method actionPerformed(ActionEvent)
    So check the spelling of the method you thought was actionPerformed().

    Sometimes people try and guard against this by using the @Override annotation.

    Java Code:
    private class buttonHandler implements ActionListener
    {
        @Override
        public void actionPeformed(ActionEvent e)
        {
            textField.setText("Good!!!!(:");
        }
    }
    @Override means "the following method must be declared in the super type". Written this way the compiler will alert you to the source of the problem because actionPeformed() is not declared in ActionListener.

Similar Threads

  1. Help with Abstract Classes please.
    By fatabass in forum New To Java
    Replies: 1
    Last Post: 03-23-2012, 04:26 AM
  2. Abstract classes
    By FadedAura in forum New To Java
    Replies: 4
    Last Post: 11-21-2011, 11:16 PM
  3. Abstract Classes and ArrayLists.
    By Hooky75 in forum New To Java
    Replies: 9
    Last Post: 05-14-2011, 10:38 AM
  4. Abstract Classes.
    By maknib in forum New To Java
    Replies: 3
    Last Post: 05-12-2011, 03:30 PM
  5. abstract classes
    By renju krishnan in forum New To Java
    Replies: 1
    Last Post: 09-29-2010, 09:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •