how do i check if a menu item in my JMenuBar has been clicked? for instance, i have a JMenu with an item called exit, which i need to make close the form. how do i do this?
import javax.swing.*;
public class selector_form extends JFrame {
/**
*
*/
private static final long serialVersionUID = -5963842156289770842L;
public selector_form(String name)
{
super.setTitle(name);
setupComponents(this);
super.setVisible(true);
}
private void setupComponents(JFrame frame)
{
JMenuItem file_items = new JMenuItem("Exit");
JMenuItem config_items = new JMenuItem("Preferences");
JMenuItem[] machine_items = {new JMenuItem("Refresh"),
new JMenuItem("Add Dynamically"), new JMenuItem("Remove Dynamically")
};
JMenuItem[] emulator_items = {new JMenuItem("Start Emulator"),
new JMenuItem("Stop Emulator"), new JMenuItem("Pause Emulator"),
new JMenuItem("Reset Emulator"), new JMenuItem("Crash Emulator")
};
JMenuItem[] memory_items = {new JMenuItem("View Emulator Memory"),
new JMenuItem("System Cheats"), new JMenuItem("Dump Emulator Memory"),
new JMenuItem("Edit Specific Address"),
new JMenuItem("Show Allocations"), new JMenuItem("Allocate Memory"),
new JMenuItem("DeAllocate Memory")
};
JMenuItem[] cpu_items = {new JMenuItem("Show Host Specs"),
new JMenuItem("Show Emulator Specs"),
new JMenuItem("Enable HyperThreadding")
};
JMenuItem[] about_items = {new JMenuItem("Help Contents"),
new JMenuItem("About")
};
frame.setSize(800, 600);
JMenuBar jmb = new JMenuBar();
JMenu jm_a = new JMenu("File");
JMenu jm_c = new JMenu("Config");
JMenu jm_d = new JMenu("Machines");
JMenu jm_e = new JMenu("Emulator");
JMenu jm_f = new JMenu("Memory");
JMenu jm_g = new JMenu("CPU");
JMenu jm_h = new JMenu("About");
jm_a.add(file_items);
jm_c.add(config_items);
for(JMenuItem item : machine_items)
jm_d.add(item);
for(JMenuItem item : emulator_items)
jm_e.add(item);
for(JMenuItem item : memory_items)
jm_f.add(item);
for(JMenuItem item : cpu_items)
jm_g.add(item);
for(JMenuItem item : about_items)
jm_h.add(item);
jmb.add(jm_a);
jmb.add(jm_c);
jmb.add(jm_d);
jmb.add(jm_e);
jmb.add(jm_f);
jmb.add(jm_g);
jmb.add(jm_h);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setJMenuBar(jmb);
}
}