Am trying to implement a set of 3 JRadiobutton
s that are detected using 3 ItemListener
s and a set of 2 JButton
s that are selected by 2 ActionListener
s.
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);
}
}