1

Am trying to implement a set of 3 JRadiobuttons that are detected using 3 ItemListeners and a set of 2 JButtons that are selected by 2 ActionListeners.

When the "OK" JButton is pressed the current JRadioButton selection should be detected.

I have tried to implement this by combining ActionEvent and ItemEvent into one method where if statements can be used to verify which JRadioButton is selected when "OK" is pressed.

The error message produced is method is not abstract and does not override abstract method actionPerformed(ActionEvent) in actionListener.

When the main method is made abstract it will not instantiate.

I have tried placing @Override before the public void actionPerformed method and using alt+ins to insert an override but this has not worked.

The code is as follows:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

class MainMenu extends JFrame implements ActionListener, ItemListener {

  JPanel pnl = new JPanel();
  JPanel grid = new JPanel(new GridLayout(3, 1));
  JRadioButton input1 = new JRadioButton("Enter New Information", true);
  JRadioButton input2 = new JRadioButton("Load from a CSV file");
  JRadioButton input3 = new JRadioButton("Save to a CSV file");
  ButtonGroup inputs = new ButtonGroup();
  JButton b1 = new JButton("OK");
  JButton b2 = new JButton("Cancel");
  JTextArea txtArea = new JTextArea(5, 25);

  public void actionPerformed(ActionEvent event2, ItemEvent event1) {

    txtArea.setText(event2.getActionCommand() + " Clicked");

    if (event2.getSource() == b1 && event1.getItemSelectable() == input1) {
        System.exit(0);
    }
    if (event2.getSource() == b1 && event1.getItemSelectable() == input2) {

        final JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(null);
        System.exit(0);
    }
    if (event2.getSource() == b1 && event1.getItemSelectable() == input3) {
        System.exit(0);
    }
    if (event2.getSource() == b2) {
        System.exit(0);
    }
  }

  public MainMenu() {

    super("Main Menu");
    setSize(300, 200);
    Container contentPane = getContentPane();
    ButtonGroup buttonGroup = new javax.swing.ButtonGroup();
    inputs.add(input1);
    inputs.add(input2);
    inputs.add(input3);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    add(grid);
    add(pnl);
    grid.add(input1);
    grid.add(input2);
    grid.add(input3);
    grid.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    pnl.add(b1);
    pnl.add(b2);
    pnl.add(txtArea);

    b1.addActionListener(this);
    b2.addActionListener(this);
    input1.addItemListener(this);
    input2.addItemListener(this);
    input3.addItemListener(this);
    contentPane.add("North", grid);
    contentPane.add("South", pnl);
    setVisible(true);
  }
}
aterai
  • 9,658
  • 4
  • 35
  • 44
TommyH
  • 23
  • 5
  • when you write in your code "class MainMenu extends JFrame implements ActionListener" it is like a contract - you promise that you implement the interface ActionListener, which guarantees that you have a method "public void actionPerformed(ActionEvent e1){..}"... but i don't see that method in your code.... (look out: "actionPerfomed(ActionEvent e1, ActionEvent e2){..}" is not the method that you have promised ^^ – Martin Frank Aug 04 '14 at 11:14
  • same applies for interface ItemListener... – Martin Frank Aug 04 '14 at 11:15
  • `ActionListener` does not define a method `public void actionPerformed(ActionEvent, ItemEvent)`, therefore you have failed to complete the requirements of contract – MadProgrammer Aug 04 '14 at 11:29
  • `@Override` states you expect that the following method is been overriding a method declared within the parent (or one it's parents) class or implemented `interfaces`. The fact that this fails (and this is why it's recommended to use it), tells you that you are not overriding any method within the parent class hierarchy – MadProgrammer Aug 04 '14 at 11:46
  • as well you might consider to use the search function instead of asking ... http://stackoverflow.com/search?q=Error+method+not+abstract+and+does+not+override+abstract+method – Martin Frank Aug 04 '14 at 12:16

1 Answers1

1

Basically, you are violating the contractual requirements of both the ActionListener and the JRadioButtons (as they are expecting you to pass them a reference to a class which implements the ItemListener interface)...

You can't simply "make up" call backs in this way, you must fulfil the requirements of the interfaces you implement.

In fact, you don't really care when the JRadioButtons change, you only care about them when the Ok or Cancel buttons are pressed, so you could get rid of the ItemListener requirement altogether and simply check the selected state of the JRadioButton you are interested, for example...

@Override
public void actionPerformed(ActionEvent event2) {

    txtArea.setText(event2.getActionCommand() + " Clicked");
    Object source = event2.getSource();

    if (source == b1 && input1.isSelected()) {
        System.exit(0);
    }
    if (source == b1 && input2.isSelected()) {

        final JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(null);
        System.exit(0);
    }
    if (event2.getSource() == b1 && input3.isSelected()) {
        System.exit(0);
    }
    if (event2.getSource() == b2) {
        System.exit(0);
    }
}

And as a runnable example...

import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

class MainMenu extends JFrame implements ActionListener {

    JPanel pnl = new JPanel();
    JPanel grid = new JPanel(new GridLayout(3, 1));
    JRadioButton input1 = new JRadioButton("Enter New Information", true);
    JRadioButton input2 = new JRadioButton("Load from a CSV file");
    JRadioButton input3 = new JRadioButton("Save to a CSV file");
    ButtonGroup inputs = new ButtonGroup();
    JButton b1 = new JButton("OK");
    JButton b2 = new JButton("Cancel");
    JTextArea txtArea = new JTextArea(5, 25);

    @Override
    public void actionPerformed(ActionEvent event2) {

        txtArea.setText(event2.getActionCommand() + " Clicked");
        Object source = event2.getSource();

        if (source == b1 && input1.isSelected()) {
            System.exit(0);
        }
        if (source == b1 && input2.isSelected()) {

            final JFileChooser fc = new JFileChooser();
            int returnVal = fc.showOpenDialog(null);
            System.exit(0);
        }
        if (event2.getSource() == b1 && input3.isSelected()) {
            System.exit(0);
        }
        if (event2.getSource() == b2) {
            System.exit(0);
        }
    }

    public MainMenu() {

        super("Main Menu");
        setSize(300, 200);
        Container contentPane = getContentPane();
        ButtonGroup buttonGroup = new javax.swing.ButtonGroup();
        inputs.add(input1);
        inputs.add(input2);
        inputs.add(input3);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(grid);
        add(pnl);
        grid.add(input1);
        grid.add(input2);
        grid.add(input3);
        grid.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        pnl.add(b1);
        pnl.add(b2);
        pnl.add(txtArea);

        b1.addActionListener(this);
        b2.addActionListener(this);
        input1.addActionListener(this);
        input2.addActionListener(this);
        input3.addActionListener(this);
        contentPane.add("North", grid);
        contentPane.add("South", pnl);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                MainMenu frame = new MainMenu();
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }

}

You might want to take a closer look at the Interfaces trail and How to Use Buttons, Check Boxes, and Radio Buttons

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Many thanks removing "implements ItemListener" from class MainMenu and "ItemEvent event1" from public void actionPerfomed worked in conjunction with adding "input1.isSelected()" etc as an alternative way of determining which JRadiobutton is currently selected. – TommyH Aug 05 '14 at 13:37