Menu Event

I have a "Exit" JMenu in JMenuBar (I set X as Mnemonic)

I wana exit (Sysytem.exit(0)) from my application when I click on Exit button OR when I press ALT+X. I dont want to use menuSelected() function of MenuListener

Please tell me which event I should use.

Thanks

Gajesh

[301 byte] By [gajesha] at [2007-11-27 11:13:04]
# 1

how about an action listener?

bsampieria at 2007-7-29 13:58:33 > top of Java-index,Desktop,Core GUI APIs...
# 2

Read the Swing tutorial on "How to Use Menus":

http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html

It shows you how to use mnemonics.

camickra at 2007-7-29 13:58:33 > top of Java-index,Desktop,Core GUI APIs...
# 3

I know that how to use Mnemonics in Menus

My main problem is related to event of Menus

Following events are not working:-

(i.e. Folowing r not solved my problem)->

actionPerformed()

menuSelected()

menuDeselected()

mouseClicked()

gajesha at 2007-7-29 13:58:33 > top of Java-index,Desktop,Core GUI APIs...
# 4

Probably best to post a Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour. This will be without the usual form funcuality, but rather just the menu function. You can find out more about this construct here:

url http://homepage1.nifty.com/algafield/sscce.html

petes1234a at 2007-7-29 13:58:33 > top of Java-index,Desktop,Core GUI APIs...
# 5

i am also new to this stuff but if you would like to use some keyboard keys you can use something like this

public void keyPressed(KeyEvent e) {

int keyCode = e.getKeyCode(); //gets the user input for the key ex 'A' or 'B' or...

//now you can use your if / else execution

if(keyCode==KeyEvent.VK_CONTROL && keyCode==KeyEvent.VK_F3) //VK_CONTROL means "ctrl" or VK_SHIFT means "shift key"

//display the message in the textField that "YOu are right"

else {

//display the output "You are wrong"

}

}

Learn about keyevents like camickr posted a tutorial where you will learn more but why not just use the dispose() method if a user clicks on exit?

JMenuItem m = (JMenuItem)e.getSource();

if ( m == quitItem ) {

dispose();

.

.

.

lrngjavaa at 2007-7-29 13:58:33 > top of Java-index,Desktop,Core GUI APIs...
# 6

if you're adding a JMenu for your 'Exit' it won't work the way you have described

Instead add a JMenuItem to your JMenuBar, but, as well as setting the Mnemonic,

set the accelerator

JMenuItem menu = new JMenuItem("Exit");

menu.setMnemonic('X');

menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,KeyEvent.ALT_DOWN_MASK));

Michael_Dunna at 2007-7-29 13:58:33 > top of Java-index,Desktop,Core GUI APIs...
# 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

}

gajesha at 2007-7-29 13:58:33 > top of Java-index,Desktop,Core GUI APIs...
# 8

Read the Swing tutorial on "How to Use Menus". It shows you how to do everything.

Menus are not designed to be used the way you want them to be used. You simply click on a Menu and a drop down list of JMenuItems is displayed. You add your Actions and Accelerators to the menu items, not the menu. This is how all standard applications are designed.

If you want to design a non-standard application, then you can try adding a JButton or JMenuItem directly to the menu bar.

camickra at 2007-7-29 13:58:33 > top of Java-index,Desktop,Core GUI APIs...
# 9

PROB. IS SOLVED

hi camickr

Only u has understood my problem exactly and provide me better solution.

I m very Thankful to user: camickr.

Here is solution:-

javax.swing.JMenuItem exit;

exit=new javax.swing.JMenuItem("Exit");

exit.setMnemonic('x');

menuBar.add(exit);

exit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

setVisible(false);

dispose();

System.exit(0);

}

});

But this will generate an runtime exception when we try to reach exit menu-item using arrow key in Menu-bar

so I will take exit JMenuItem in a JMenu

Nice to meet u

gajesha at 2007-7-29 13:58:33 > top of Java-index,Desktop,Core GUI APIs...
# 10

@OP: see comment below

exit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// remove setvisible and dispose no need because only system.exit(0) is needed.

setVisible(false);

dispose();

System.exit(0);

}

});

Message was edited by:

Yannix

Yannixa at 2007-7-29 13:58:33 > top of Java-index,Desktop,Core GUI APIs...