473,700 Members | 2,536 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 948

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

Similar topics

3
2707
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
6221
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
3243
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
7711
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
1916
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
5747
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
951
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
5287
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
8712
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
8639
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9203
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...
1
8952
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
7794
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...
0
5895
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
4395
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4649
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2375
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.