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);
}
}
Basically, you are violating the contractual requirements of both the ActionListener
and the JRadioButton
s (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 JRadioButton
s 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