# 7
hi petes1234
thanks for knowing me about SSCCE
as per advising u...
i m sending u my compilable code...
i know that how to set mnemonics with Menus and also providing short-cut keys to Menus
Actually I wants:
1. when I click by Mouse on a menu in Menu-Bar then System.exit(0)
2. when I press hot-key for menu (like ALT+X) then System.exit(0)
3. when I select this perticular menu using arrow-keys then after pressing enter key, System.exit(0)
Is all these things possible using one event?
If yes, then plz tell me. I have already checked then menuKeyPressed(), menuSelected(),menuDeselected(),and actionPerformed() functions..
If any one interested to solve my probs then please mail me at <gajeshtripathi@sangamflock.com> otherwise u can reply me using this forum
source is here...
===============
/*
* FrmLabMain.java
*
* Created on July 20, 2007, 1:45 PM
*/
import java.io.File;
import java.io.IOException;
import javax.swing.ImageIcon;
/**
*
* @author sprtriga (Gajesh Tripathi)
*/
public class FrmLabMain extends javax.swing.JFrame {
/**
* Creates new form FrmLabMain
*/
public FrmLabMain() {
initComponents();
setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
menuBar = new javax.swing.JMenuBar();
mnuMaster = new javax.swing.JMenu();
testMI = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JSeparator();
groupMI = new javax.swing.JMenuItem();
subGroupMI = new javax.swing.JMenuItem();
mnuTransaction = new javax.swing.JMenu();
resultMI = new javax.swing.JMenuItem();
mnuReports = new javax.swing.JMenu();
mnuExit = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Soni Hospital Laboratory");
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setResizable(false);
mnuMaster.setMnemonic('M');
mnuMaster.setText("Master");
testMI.setMnemonic('L');
testMI.setText("Lab Test");
mnuMaster.add(testMI);
mnuMaster.add(jSeparator1);
groupMI.setMnemonic('G');
groupMI.setText("Test Group");
mnuMaster.add(groupMI);
subGroupMI.setMnemonic('S');
subGroupMI.setText("Sub Group");
mnuMaster.add(subGroupMI);
menuBar.add(mnuMaster);
mnuTransaction.setMnemonic('T');
mnuTransaction.setText("Transaction");
resultMI.setMnemonic('R');
resultMI.setText("Test Result");
mnuTransaction.add(resultMI);
menuBar.add(mnuTransaction);
mnuReports.setMnemonic('R');
mnuReports.setText("Reports");
menuBar.add(mnuReports);
mnuExit.setMnemonic('x');
mnuExit.setText("Exit");
mnuExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mnuExitActionPerformed(evt);
}
});
mnuExit.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
mnuExitMouseClicked(evt);
}
});
menuBar.add(mnuExit);
setJMenuBar(menuBar);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void mnuExitActionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
dispose();
System.exit(0);
}
/*
* This event is not working when I use Keyboard
* mnuExitMenuKeyPressed(..) is also not working
* NOTE: I m not satisfy with mnuExitMenuSelected(...) or mnuExitMenuDeselected(...) event-function
*/
private void mnuExitMouseClicked(java.awt.event.MouseEvent evt) {
setVisible(false);
dispose();
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FrmLabMain().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem groupMI;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenu mnuExit;
private javax.swing.JMenu mnuMaster;
private javax.swing.JMenu mnuReports;
private javax.swing.JMenu mnuTransaction;
private javax.swing.JMenuItem resultMI;
private javax.swing.JMenuItem subGroupMI;
private javax.swing.JMenuItem testMI;
// End of variables declaration
}