473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

help with GUI assign a keystroke

nomad
664 Expert 512MB
I'm trying to figure out how to assign a keystroke and a click to a menu item.
Example If a user click on a menu and selects the Close the program
Menu and chose Close[/b]. I want the program to open (//inner class Section used for exit on menu 3 for fun) ie "Exit Program".

here is my code so far.


Expand|Select|Wrap|Line Numbers
  1. public class GUIMenu extends JFrame {
  2.  
  3.     public GUIMenu() {
  4.  
  5.         super("FeedBar 2");
  6.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  7.  
  8.      //Build third menu in the menu bar.
  9.         JMenuItem a1 = new JMenuItem("Close");
  10.         a1.setMnemonic(KeyEvent.VK_D);
  11.  
  12. //Create the menu bar.
  13.         JMenuBar menubar = new JMenuBar();
  14.  
  15.    //Build the Third menu.
  16.         JMenu menu3 = new JMenu("Close the program");
  17.         menu3.add(a1);
  18.  
  19.         menubar.add(menu3);
  20.  
  21.         // prepare user interface
  22.         JTextArea edit = new JTextArea(8, 40);
  23.         JScrollPane scroll = new JScrollPane(edit);
  24.         BorderLayout bord = new BorderLayout();
  25.         setLayout(bord);
  26.  
  27.         add("Center", scroll);
  28.         setJMenuBar(menubar);
  29.         pack();
  30.         setVisible(true);
  31.     }
  32.  
  33.     //inner class Section used for exit on menu 3 for fun
  34.     class exitListener implements ActionListener {
  35.         public void actionPerformed(ActionEvent arg0) {
  36.  
  37.                 int x = JOptionPane.showOptionDialog(GUIMenu.this, "Exit Program?",
  38.                                 "Exit Request", JOptionPane.YES_NO_OPTION,
  39.                                 JOptionPane.QUESTION_MESSAGE, null, null,
  40.                                 JOptionPane.NO_OPTION);
  41.                 if (x == JOptionPane.YES_OPTION) {
  42.                     GUIMenu.this.dispose();
  43.                 }//close the if statement
  44.         }//close public void actionPerformed
  45. }//close the class exitListener
  46.  
  47.  
  48.     public static void main(String[] arguments) {
  49.         GUIMenu frame = new GUIMenu();
  50.     }
  51. }
Thanks
nomad
PS I know I need add a listener of some type but I don't know where or what kind.
May 4 '07 #1
0 939

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ste | last post by:
Hello, my goal would be implementing some keystroke analysis using php. I'm a novice in php, so i ask your advice: according to you, is it possible to log each keystroke client-side even if php is...
7
by: hokieghal99 | last post by:
Does anyone know of a keystroke logger that has been written in Python for Windows machines? I'd like to see such a script and use it as a point of reference for a real-time backup project that I'm...
2
by: Wayne Wengert | last post by:
I want to get the decimal (integer) value of user keystrokes. I am using the following code. chrInput is the character entered (e.g. "M") and "keystroke is DIMed as Short keystroke =...
8
by: Lucy | last post by:
help! javascript inline of an HTML to open an HTML in a target window. Something like... <html> <head> <title>test</title> </head> <body>
1
by: Amin Gunawan via .NET 247 | last post by:
I would like to put a combo box in a column of a datagrid. I've seen the samples in the MSDN library of other sources, butall samples didn't catch the 'DOWN/UP' keystroke. It means thatwhen I select...
15
by: Frank Bormann | last post by:
Hi, probably a stupid question, but I haven't been able to find anything. Is there a istream related function that let me read exactly one keystroke from the keyboard through cin? What I...
1
by: Me | last post by:
I have a Windows application (app1). When I launch it, it starts a process (myP0) that runs another application (app2). By default app1 starts minimized and is not visible in the system tray. What...
1
by: Joseph Geretz | last post by:
I'm noticing that when my DataGrid has focus, it handles the Enter key by advancing to the next row in the grid. This neutralizes Forms's current defined AcceptButton. My primary question: is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.