I'm making a very poor attempt at ActionListeners here.
I'm trying to use ActionListeners to run code from in another class (AddForm.java) once a JMenuItem (in MainMenu.java) is clicked.
First, here's the code: MainMenu.java
package carparksystem;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.awt.event.*;
public class MainMenu extends JFrame
{
public MainMenu()
{
JMenuBar mainMenu = new JMenuBar();
JMenu main = new JMenu("Menu");
mainMenu.add(main);
JMenuItem addCar = new JMenuItem("Add Car");
addCar.setActionCommand("Add");
main.add(addCar);
//addCar.addActionListener();
JMenuItem removeCar = new JMenuItem("Remove Car");
removeCar.setActionCommand("Remove");
main.add(removeCar);
JMenuItem searchCars = new JMenuItem("Search Cars");
searchCars.setActionCommand("Search");
main.add(searchCars);
setJMenuBar(mainMenu);
/*
//Add action listener for the Add Car button
addCar.addActionListener
(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
MainMenu.windowClosed();
}
}
);
*/
}
}
AddForm.java:
package carparksystem;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
public class AddForm extends JFrame
{
public AddForm()
{
JLabel regNumLabel = new JLabel("Registration Number:");
JLabel highValLabel = new JLabel("High Value?");
JLabel largeLabel = new JLabel("Large Vehicle?");
JRadioButton btnYesHighVal = new JRadioButton("Yes", false);
JRadioButton btnNoHighVal = new JRadioButton("No", true);
JRadioButton btnYesLarge = new JRadioButton("Yes", false);
JRadioButton btnNoLarge = new JRadioButton("No", true);
ButtonGroup highVal = new ButtonGroup(); //allows just one radio button from the group to be selected
highVal.add(btnYesHighVal);
highVal.add(btnNoHighVal);
ButtonGroup largeCar = new ButtonGroup(); //allows just one radio button from the group to be selected
largeCar.add(btnYesLarge);
largeCar.add(btnNoLarge);
JTextField regNumField = new JTextField();
JButton addCar = new JButton(" Add ");
JButton addCancel = new JButton("Cancel");
GroupLayout addLayout = new GroupLayout(getContentPane()); //chosen to display components in group layout
getContentPane().setLayout(addLayout);
addLayout.setAutoCreateGaps(true);
addLayout.setAutoCreateContainerGaps(true);
addLayout.setHorizontalGroup(addLayout.createSequentialGroup()
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(regNumLabel)
.addComponent(highValLabel)
.addComponent(largeLabel))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(regNumField)
.addGroup(addLayout.createSequentialGroup()
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(btnYesHighVal)
.addComponent(btnYesLarge))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(btnNoHighVal)
.addComponent(btnNoLarge))))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(addCar)
.addComponent(addCancel))
);
addLayout.setVerticalGroup(addLayout.createSequentialGroup()
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(regNumLabel)
.addComponent(regNumField)
.addComponent(addCar))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(addLayout.createSequentialGroup()
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(highValLabel)
.addComponent(btnYesHighVal)
.addComponent(btnNoHighVal))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(largeLabel)
.addComponent(btnYesLarge)
.addComponent(btnNoLarge)))
.addComponent(addCancel))
);
setSize(375, 150);
setTitle("Add Car");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
On the main frame I have drawn a series of shapes which will appear on the Frame at first. And there will be a JMenu above this with the JMenuItems Add, Remove and Search Cars in the menu.
Once these Add Remove and Search 'menu buttons' are clicked they will open the corresponding form which will allow the user to input data.
When I run my code with and without the action listeners, it runs as normal but the menus don't link at all. It's as if they have no meaning?