473,750 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with GUI assign a keystroke

nomad
664 Recognized Expert Contributor
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 951

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

Similar topics

3
2712
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 a server-side language, or shall i use JavaScript and make the client run the program locally? And, if i can use php to do that, how can i implement a feature logging keystrokes on the client system? Thanks,
7
6224
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 working on. Basically, I'd like to be able to capture all keystrokes from the keyboard buffer and write them to a text file so I could reporduce emails, documents, etc. in the event of file loss that occurs between nightly backups. For example,...
2
3246
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 = Microsoft.VisualBasic.Val(chrInput) but it always yields a value of 0 How should I be doing this?
8
7714
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
1919
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 an item in the combo box, I need to use my mousefor the selection. If I use the 'DOWN/UP' keystroke, the cell inthe grid move to the next or prev row in the grid. Anybody knows how to catch the down/up keystroke when selectingitems in the combo...
15
5757
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 need it to do is this: - remove all characters currently in the input buffer - block until the user presses a key
1
952
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 I want to do is code it so that when the users presses the ESC key the keystroke is captured by app1 and the app1 kills the process that is running app2. Killing app2 is not a problem. However, since app2 has the focus, how do I get app1 to...
1
5290
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 there any quick way to configure the Grid not to handle the Enter key? I do not see any property control which might configure this behavior. My secondary question: If there's no quick solution via Grid configuration, then how do I get the...
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6804
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.